[英]xdocument.root.element and other permutations return null
flow.Name绝对等于flowData XDocument中流之一的“名称”。
XElement rootelem = flowData.Root.Element("flows");
在上面的行之后,rootelem包含flows元素,并且它是预期的子元素,但下面的行引发空引用异常,为什么?
flowData.Root.Element(flow.Name).Remove();
flowData被声明为XDocument ,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
-<D53ESB>
-<comms>
<diagnosticemails sender="eventlog"/>
</comms>
-<globalparams>
<!-- some comments... -->
</globalparams>
-<flows>
-<flow webserviceonly="false" stoponerror="true" name="testFlow">
-<action name="t1">
<schedule firsttime="01/01/2014 14:10:00" every="600000"/>
-<adapter name="GetXml">
<param name="url" value="http://xml.betfred.com/Football-Championship.xml"/>
</adapter>
</action>
</flow>
...more flows
</flows>
</D53ESB>
这两行也返回null:
var xelem2 = flowData.Root.Element(flow.Name);
var xelem3 = flowData.Root.Element("flows").Element(flow.Name);
这两个返回空集:
var keepgoing = new XDocument(rootelem.Descendants(flow.Name));
var idk = new XDocument(flowData.Descendants(flow.Name));
XElement.Element
方法需要元素名称,而不是属性值。 它不知道哪个属性值是元素的名称。
你应该试试:
flowData.Root.Element("flows")
.Elements("flow")
.Where(f => (string)f.Attribute("name") == flow.Name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.