C# 缺少根元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10125810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:32:04  来源:igfitidea点击:

Root element is missing

c#asp.netxml

提问by R.D.

I am reading xml from xxx URl but i am getting error as Root element is missing.

我正在从 xxx URl 读取 xml,但由于缺少 Root 元素而出现错误。

My code to read xml response is as follows:

我读取xml响应的代码如下:

  XmlDocument doc = new XmlDocument();
  doc.Load("URL from which i am reading xml");
  XmlNodeList nodes = doc.GetElementsByTagName("Product");
  XmlNode node = null;
  foreach (XmlNode n in nodes)
   {
   }

and the xml response is as follows:

xml响应如下:

<All_Products>
   <Product>
  <ProductCode>GFT</ProductCode>
  <ProductName>Gift Certificate</ProductName>
  <ProductDescriptionShort>Give the perfect gift. </ProductDescriptionShort>
  <ProductDescription>Give the perfect gift.</ProductDescription>
  <ProductNameShort>Gift Certificate</ProductNameShort> 
  <FreeShippingItem>Y</FreeShippingItem>
  <ProductPrice>55.0000</ProductPrice>
  <TaxableProduct>Y</TaxableProduct>
   </Product>    
 </All_Products>

Can you please tell where i am going wrong.

你能告诉我哪里出错了。

采纳答案by Raghuveer

Hi this is odd way but try it once

嗨,这是奇怪的方式,但尝试一次

  1. Read the file content into a string
  2. print the string and check whether you are getting proper XML or not
  3. you can use XMLDocument.LoadXML(xmlstring)
  1. 将文件内容读入字符串
  2. 打印字符串并检查您是否获得了正确的 XML
  3. 您可以使用 XMLDocument.LoadXML(xmlstring)

I try with your code and same XML without adding any XML declaration it works for me

我尝试使用您的代码和相同的 XML 而不添加任何适用于我的 XML 声明

XmlDocument doc = new XmlDocument();
        doc.Load(@"H:\WorkSpace\C#\TestDemos\TestDemos\XMLFile1.xml");
        XmlNodeList nodes = doc.GetElementsByTagName("Product");
        XmlNode node = null;
        foreach (XmlNode n in nodes)
        {
            Console.WriteLine("HI");
        }

Its working perfectly fine

它工作得很好

回答by Tom Hazel

If you are loading the XML file from a remote location, I would check to see if the file is actually being downloaded correctly using a sniffer like Fiddler.

如果您是从远程位置加载 XML 文件,我将使用Fiddler 之类的嗅探器检查该文件是否确实被正确下载。

I wrote a quick console app to run your code and parse the file and it works fine for me.

我编写了一个快速控制台应用程序来运行您的代码并解析文件,它对我来说很好用。

回答by coder

Make sure you XML looks like this:

确保您的 XML 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<rootElement>
...
</rootElement>

Also, a blank XML file will return the same Root elements is missing exception. Each XML file must have a root element / node which encloses all the other elements.

此外,空白 XML 文件将返回相同的 Root 元素缺失异常。每个 XML 文件都必须有一个包含所有其他元素的根元素/节点。

回答by Phil

Just in case anybody else lands here from Google, I was bitten by this error message when using XDocument.Load(Stream) method.

以防万一其他人从谷歌登陆这里,我在使用 XDocument.Load(Stream) 方法时被这个错误消息咬了。

XDocument xDoc = XDocument.Load(xmlStream);  

Make sure the stream position is set to 0 (zero) before you try and load the Stream, its an easy mistake I always overlook!

在尝试加载流之前,请确保将流位置设置为 0(零),这是我总是忽略的一个简单错误!

if (xmlStream.Position > 0)
{
    xmlStream.Position = 0;
}
XDocument xDoc = XDocument.Load(xmlStream); 

回答by Idoshhh

  1. Check the trees.config file which located in config folder... sometimes (I don't know why) this file became to be empty like someone delete the content inside... keep backup up of this file in your local pc then when this error appear - replace the server file with your local file. This is what i do when this error happened.

  2. check the available space on the server. sometimes this is the problem.

  1. 检查位于 config 文件夹中的 tree.config 文件......有时(我不知道为什么)这个文件变得空了,就像有人删除了里面的内容......在你的本地电脑上备份这个文件然后当出现此错误 - 用您的本地文件替换服务器文件。这就是我在发生此错误时所做的。

  2. 检查服务器上的可用空间。有时这就是问题所在。

Good luck.

祝你好运。

回答by Dmitry Potapov

I had the same problem when i have trying to read xml that was extracted from archive to memory stream.

当我试图读取从存档中提取到内存流的 xml 时,我遇到了同样的问题。

 MemoryStream SubSetupStream = new MemoryStream();
        using (ZipFile archive = ZipFile.Read(zipPath))
        {
            archive.Password = "SomePass";
            foreach  (ZipEntry file in archive)
            {
                file.Extract(SubSetupStream);
            }
        }

Problem was in these lines:

问题出在这些行中:

XmlDocument doc = new XmlDocument();
    doc.Load(SubSetupStream);

And solution is (Thanks to @Phil):

解决方案是(感谢@Phil):

        if (SubSetupStream.Position>0)
        {
            SubSetupStream.Position = 0;
        }