C# XmlDocument.Load 与 XmlDocument.LoadXml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1660676/
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
XmlDocument.Load Vs XmlDocument.LoadXml
提问by Sergio
I just came across with a problem using XmlDocument.LoadXml
.
我刚刚在使用XmlDocument.LoadXml
.
The application was crashing, giving the following error:
应用程序崩溃,出现以下错误:
"Data at the root level is invalid. Line 1, position 1"
“根级别的数据无效。第 1 行,位置 1”
After inspecting the XML and finding nothing wrong with it, I googled a bit and found a tip to use XmlDocument.Load
instead of XmlDocument.LoadXml
.
在检查了 XML 并发现它没有任何问题后,我用谷歌搜索了一下,找到了一个可以XmlDocument.Load
代替XmlDocument.LoadXml
.
I have tried it and it works perfectly.
我已经试过了,效果很好。
My question is: What is the difference between the 2 methods and what could have cause one to work and the other to fail?
我的问题是:这两种方法有什么区别,什么可能导致一种方法工作而另一种方法失败?
采纳答案by AdaTheDev
XmlDocument.Loadis used to load XML either from a stream, TextReader, path/URL, or XmlReader. XmlDocument.LoadXmlis used to load the XML contained within a string.
XmlDocument.Load用于从流、TextReader、路径/URL 或 XmlReader 加载 XML。 XmlDocument.LoadXml用于加载包含在字符串中的 XML。
They're fundamentally different ways of loading XML, depending on where the XML is actually stored. So it sounds like you were using the wrong method for where your XML is.
它们是加载 XML 的根本不同方式,具体取决于 XML 的实际存储位置。因此,听起来您对 XML 所在的位置使用了错误的方法。
回答by Jon Skeet
Were you trying to use XmlDocument.LoadXml
and passing in the name of a file? It doesn't do that - it assumes that the string you pass in isthe XML. So you might use:
您是否尝试使用XmlDocument.LoadXml
并传入文件名?它不这样做 - 它假定您传入的字符串是XML。所以你可能会使用:
doc.LoadXml("<root><child /><root>");
or
或者
doc.Load("myfile.xml");
If that doesn't help, could you show your failing and working code? There are different ways you could have changed from using LoadXml
to Load
...
如果那没有帮助,你能展示你的失败和工作代码吗?您可以通过不同的方式从使用LoadXml
变为Load
...
回答by anonymous coward
Load() loads from a certain source, whereas LoadXml() loads directly from a string
Load() 从某个源加载,而 LoadXml() 直接从字符串加载
回答by bruno conde
Assuming your using XmlDocument.Load
and XmlDocument.LoadXml
in the rightway this problem may be caused by Byte Order Mark.
假设你的使用XmlDocument.Load
,并XmlDocument.LoadXml
在正确的方式这个问题的原因可能有字节顺序标记。
This other questionmight be useful.
这个另一个问题可能有用。
回答by Johnson TONG
The application was crashing with the following error: "Data at the root level is invalid. Line 1, position 1" I suspect you xml data does not have a root level: for example:
应用程序因以下错误而崩溃:“根级别的数据无效。第 1 行,位置 1”我怀疑您的 xml 数据没有根级别:例如:
<area id="1">
<candidate id="0">dataata</candidate>
</area>
<area id="2">
<candidate id="0">dataataa</candidate>
</area>
you need have at least one root level on top of the bottom levels. for example:
您需要在底层之上至少有一个根级别。例如:
<areas>
<area id="1">
<candidate id="0">dataata</candidate>
</area>
<area id="2">
<candidate id="0">dataataa</candidate>
</area>
</areas>
so please put one mother on the top of your level, make it grand grand mother of all children
所以请在你的水平上放一位母亲,让它成为所有孩子的祖母