繁体   English   中英

尝试在两个Xml文档之间进行联接时出现NullReferenceException

[英]NullReferenceException when trying to do a Join between two Xml Documents

我有一个看起来像这样的Xml文档(该文档显示了更多的父母和孩子):

<item cid="0x12e3">
    <item cid="0x1310">
        <item cid="0x158b">
            <item luid="2001"/>
            <item luid="2002"/>
        </item>
        <item cid="0x13313">
            <item luid="2001"/>
            <item luid="2002"/>
        </item>
        ... 
    </item>
</item>

并且我还有一个Xml文档,其中包含一些这样的属性:

<?xml version="1.0" encoding="UTF-8"?>
<EditorRule>
     <Properties>
         <Property name="Comment" luid="2001"/>
         ...
     </Properties>
</EditorRule>

现在,我正在做一个Join的两个文件之间进行SelectXElementsfirst xml正在对发现的Second xml

(from element in Element.Elements().Elements().Elements()
            join property in propertiesFromFile on element.Attribute("luid").Value equals property.Luid 
            select new IpjItem(element));

这可以正常工作,并且当我遍历此IEnumerable它将打印Name="Comment" ,依此类推。

我的问题是,它仅适用于第一个XElement. 因此,回到第first Xml 它仅从cid = 0x158b的节点打印Children cid = 0x158b 完成该元素后,它将引发NullReferenceException ,并且不会转到cid = 0x13313 为什么会这样呢?

谢谢!

是。 正如@Selman22意外推断的那样,我的问题出在Element.Elements() 我还添加了一个更好的Where子句来检查属性是否存在。

return (from element in Element.Elements()
            where element.Attribute("luid") != null
            join property in RuleEditorXml.Properties on element.Attribute("luid").Value equals property.Luid
            select new IpjItem(element, Parent));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM