Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XDocument不是&x27;不返回任何值_C#_Xml_Linq To Xml - Fatal编程技术网

C# XDocument不是&x27;不返回任何值

C# XDocument不是&x27;不返回任何值,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我正在从一个文件中读取一些XML。在文件中,XML存储为一行文本,如下所示: <ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><KeyValueOfstringanyType><Key>askforreview&

我正在从一个文件中读取一些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
换句话说,看起来所有的值都一起运行了

更新:我一直试图做的事情的清晰性…

考虑到有关名称空间的建议,我尝试了以下方法:

oldSettings = XDocument.Load(sr);
XNamespace f ="http://schemas.microsoft.com/2003/10/Serialization/Arrays";
XElement f1 = oldSettings.Element(f + "KeyValueOfstringanyType");
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();
(请原谅var名称-这只是在即时窗口中)

将f1返回为null。如果我尝试:

f1 = oldSettings.Element(f + "ArrayOfKeyValueOfstringanyType");
然后将f1设置为整个XML文档,因为它是根元素。调用f1.subjections()后,我再次一无所获

我引用的名称空间是否正确?ArrayOfKeyValueOfstringanyType似乎有两个-一个是我在上面使用的,另一个是以“xmlns:I=”开头的https://..."

如果我尝试:

f1 = oldSettings.Element(f + "ArrayOfKeyValueOfstringanyType");
var f2 = oldSettings.Elements(f + "KeyValueOfstringanyType");

我得到空值

按照中给出的示例,我还尝试了以下方法:

oldSettings = XDocument.Load(sr);
XNamespace f ="http://schemas.microsoft.com/2003/10/Serialization/Arrays";
XElement f1 = oldSettings.Element(f + "KeyValueOfstringanyType");
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();
xns=”http://schemas.microsoft.com/2003/10/Serialization/Arrays";
IEnumerable kv=旧设置中的el。元素(xns+“KeyValueOfstringanyType”)
选择el;
var f4=千伏ToList();
f4也是空的

我需要做什么才能让它工作?我需要重新格式化XML以引入新行字符吗?(文件不是我创建的)


谢谢。

看看您尝试过的代码,似乎您只是运气不好:p您尝试访问的元素是根元素的直接子元素,因此此元素将获得所需的元素:

XElement f1 = oldSettings.Root.Element(f + "KeyValueOfstringanyType");
如果使用
子体
,则需要调用
ToList()
ToArray()
,或遍历结果以查看实际内容:

var f2 = oldSettings.Descendants(f + "KeyValueOfstringanyType").ToList();

使用
子体()

时的问题与您的问题类似,您只是犯了一些简单的错误:

XElement f1 = oldSettings.Element(f + "KeyValueOfstringanyType");
应该改成

XElement f1 = oldSettings.Root.Element(f + "KeyValueOfstringanyType");
因为您正在查找根元素下面的元素

var f2 = oldSettings.Descendants(f + "KeyValueOfstringanyType");

将返回预期的元素且不为null,因此您的代码中肯定存在som拼写错误。

您没有提供任何代码,因此很难给出任何建议。但是,您的代码是否考虑了名称空间?谢谢,Martin。阅读了链接的so Q&A,我现在更新了我的问题的更多细节。
var f2 = oldSettings.Descendants(f + "KeyValueOfstringanyType");