English 中文(简体)
程序集XML文件无效
原标题:Invalid Assembly XML File
  • 时间:2010-10-21 18:53:30
  •  标签:
  • c#
  • .net
  • xml


I am trying to read the XML Documentation file (C#) using this ocde -
Type classType = typeof(Point);

        string documentationFileLocation = classType.Assembly.CodeBase;
        if ( !string.IsNullOrEmpty(documentationFileLocation) && documentationFileLocation.StartsWith("file:///") )
        {
            documentationFileLocation = documentationFileLocation.Replace(".exe",".xml");
            documentationFileLocation = documentationFileLocation.Replace("file:///","");
            if(File.Exists(documentationFileLocation))
            {

                XElement document = XElement.Load(documentationFileLocation);
                // Some Code Logic Here using LINQ
            }
            else
            {
                Console.WriteLine("Please Go to Project Properties->Build and check  XML Documentation file ");

I have a LINQ Query after XElement document = XElement.Load(sr) which dosen`t work,
So I put a breakpoint in the LINQ Query and I am getting this error -
XmlException - Data at the root level is invalid. Line 1, position 1.

我该怎么修?

编辑:稍微更改了代码-刚刚删除了StreamReader

最佳回答

您是否尝试过XDocument.Load()而不是使用XElement?如果文件以XML声明开头<;?xml中,在尝试从中加载元素时可能会出现此错误。

编辑:粘贴在粘贴器上的文件没有指定编码。你能试着在记事本中打开这个文件,并将其重新保存为ANSI,看看它是否加载了吗?只是为了确保我们没有编码或BOM问题。

问题回答

听起来它根本不是一个有效的XML文件。

如果您打印出sr.ReadToEnd()的结果,而不是调用XElement.Load,它看起来是什么样子?如果您试图将文件加载到XML编辑器中,会发生什么情况?

顺便说一句,使用使用语句比显式调用Dispose更好:对于当前代码,如果Load抛出异常,则不会处理StreamReader

最后,您不只是使用XElement.Load(documentationFileLocation)有什么原因吗?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...