XDocument不返回任何值
本文关键字:任何值 返回 XDocument | 更新日期: 2023-09-27 18:16:29
我正在从文件中读取一些XML。在该文件中,XML存储为一行文本,如下所示:
<ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><KeyValueOfstringanyType><Key>askforreview</Key><Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:boolean">false</Value></KeyValueOfstringanyType><KeyValueOfstringanyType><Key>started</Key><Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:int">1</Value></KeyValueOfstringanyType><KeyValueOfstringanyType><Key>LastRunVersion</Key><Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:int">148</Value></KeyValueOfstringanyType> ... etc
我正在用StreamReader读取文件,调用XDocument.Load。对于调试中的代码,如果我转储XDocument变量,Visual Studio将以格式化的方式打印出XML,这表明XDocument已经正确地解析了单行文本:
<ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<KeyValueOfstringanyType>
<Key>askforreview</Key>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:boolean">false</Value>
</KeyValueOfstringanyType>
<KeyValueOfstringanyType>
<Key>started</Key>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:int">1</Value>
</KeyValueOfstringanyType>
<KeyValueOfstringanyType>
<Key>LastRunVersion</Key>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:int">148</Value>
</KeyValueOfstringanyType>
etc
然而,试图提取元素,后代,值等几乎都导致返回null。我担心的是,如果调用.Root,将再次显示所有XML,以及以下内容:
FirstAttribute: {xmlns:i="http://www.w3.org/2001/XMLSchema-instance"}
HasAttributes: true
HasElements: true
IsEmpty: false
LastAttribute: {xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"}
Name: {{http://schemas.microsoft.com/2003/10/Serialization/Arrays}ArrayOfKeyValueOfstringanyType}
NodeType: Element
Value: "askforreviewfalsestarted1LastRunVersion148 ... etc"
换句话说,看起来所有的值都是一起运行的?
更新:我一直在努力做的事情的清晰度
给出了关于名称空间的建议,我尝试了这样做:
oldSettings = XDocument.Load(sr);
XNamespace f ="http://schemas.microsoft.com/2003/10/Serialization/Arrays";
XElement f1 = oldSettings.Element(f + "KeyValueOfstringanyType");
(请原谅var名称-这只是在即时窗口)
返回f1为空。如果我尝试:
f1 = oldSettings.Element(f + "ArrayOfKeyValueOfstringanyType");
则f1被设置为整个XML文档,因为它是根元素。调用f1.Descendants()也不会得到任何结果。
我引用了正确的命名空间吗?ArrayOfKeyValueOfstringanyType似乎有两个-我上面使用的一个,加上一个开始'xmlns: I ="https://...'。
如果我尝试:
var f2 = oldSettings.Elements(f + "KeyValueOfstringanyType");
或
var f2 = oldSettings.Descendants(f + "KeyValueOfstringanyType");
或
var f2 = oldSettings.Root.Descendants(f + "KeyValueOfstringanyType");
我得到null
按照http://msdn.microsoft.com/en-us/library/bb669152.aspx中给出的示例,我也尝试了这个:
XNamespace xns = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
IEnumerable<XElement> kv = from el in oldSettings.Elements(xns + "KeyValueOfstringanyType")
select el;
var f4 = kv.ToList();
和f4也为空。
我需要做什么才能使它工作?我是否需要重新格式化XML以引入新的行字符?(文件不是我创建的)
谢谢。
看看你尝试过的那些代码,似乎你只是不走运:p你试图访问的元素是根元素的直接子元素,所以这一个将得到所需的元素:
XElement f1 = oldSettings.Root.Element(f + "KeyValueOfstringanyType");
如果使用Descendant
,则需要调用ToList()
或ToArray()
,或者遍历结果以查看实际内容:
var f2 = oldSettings.Descendants(f + "KeyValueOfstringanyType").ToList();
问题类似于你的问题时使用Descendants()
: c# XML节点工作与元素,但不与元素
你只是犯了一些简单的错误:
XElement f1 = oldSettings.Element(f + "KeyValueOfstringanyType");
改为
XElement f1 = oldSettings.Root.Element(f + "KeyValueOfstringanyType");
,因为您正在查找根元素以下的元素。
var f2 = oldSettings.Descendants(f + "KeyValueOfstringanyType");
将返回期望的元素,而不是null,所以您的代码中一定有拼写错误。