[英]query deep into xElement using linq-xml
I have XML 我有XML
<Envelopes>
<Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<Body>
<UpdateObjectResponse xmlns="http://www.sap.com/SBO/DIS"
CommandID="UpdateObject picklist">
<RetKey>426358</RetKey>
<RetType>156</RetType>
</UpdateObjectResponse>
</Body>
</Envelope>
<Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<Body>
<UpdateObjectResponse xmlns="http://www.sap.com/SBO/DIS"
CommandID="UpdateObject picklist">
<RetKey>426358</RetKey>
<RetType>156</RetType>
</UpdateObjectResponse>
</Body>
</Envelope>
</Envelopes>
and I am trying to get value of RetKey
element like so 我试图像这样获取
RetKey
元素的值
var query = from t in xdoc.Descendants("Envelope") select t;
foreach (XElement item in query)
{
var k = item.Element("Body").Element("UpdateObjectResponse").Element("RetKey").Value;
}
var query
and item
are getting set properly, but I am getting this error within the foreach
loop var
query
和item
已正确设置,但我在foreach
循环中收到此错误
"Object reference not set to an instance of an object." “你调用的对象是空的。”
I cannot see anything obvious that's wrong with your code. 我看不到任何明显的代码错误。
I'll assume you're using VS? 我假设您使用的是VS? Try running the following code with a break point in each line of the foreach and chech the locals at each step, to see which of the .Element(..) queries might be the problem.
尝试在foreach的每一行中运行带有断点的以下代码,并在每一步检查本地变量,以查看可能是哪个.Element(..)查询。 Check each of the variables in in the locals view at each step, to see if something unexpected appears in one of the XElements.
在每个步骤中,请在locals视图中检查每个变量,以查看是否有意外情况出现在XElement之一中。
var query = from t in xdoc.Descendants("Envelope") select t;
foreach (XElement item in query)
{
var j = item;
var k = item.Element("Body");
var l = k.Element("UpdateObjectResponse");
var m = l.Element("RetKey");
var n = m.Value;
}
Either you will see which of the variables jklmn is different than expected, or the code will break with a more informative exception. 您可能会看到jklmn中的哪个变量与预期的不同,或者代码将以更多信息性异常中断。
Note: at least in VS2015, there's a little magnifying glass in the locals view, which lets you nicely view XMLs (in case you didn't know). 注意:至少在VS2015中,locals视图中有一个放大镜,可让您很好地查看XML(以防万一)。
Edit : I was not aware of the differences between XElement.Descendants and XElement.Elements and I am still unclear on how to do this properly with the cascaded namespaces. 编辑 :我不知道XElement.Descendants和XElement.Elements之间的区别 ,我仍然不清楚如何正确地使用级联名称空间。 See Without or only root namespace .
请参阅不使用或仅使用根名称空间 。 See Strip namespaces from Elements in case the namespace isn't fixed.
如果名称空间不固定,请参见从Elements中剥离名称空间。
I think that this might be the solution, though. 我认为这可能是解决方案。 If I get it correctly, you can just write
如果我没弄错,你可以写
XNamespace ns = "http://www.sap.com/SBO/DIS";
var klist = from t in xdoc.Descendants(ns + "RetKey") select t.Value;
to get a list of the RetKey values in that xml. 获取该xml中RetKey值的列表。 Maybe also try xdoc.Root.Descendants to avoid the first namespace.
也许还尝试使用xdoc.Root.Descendants来避免第一个名称空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.