简体   繁体   English

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

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

I have an Xml document that looks something like this (It contains more parents and children that the shown): 我有一个看起来像这样的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>

and Also I have an Xml document with some properties like this: 并且我还有一个Xml文档,其中包含一些这样的属性:

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

Now, I'm doing a Join between the two files to Select the XElements from the first xml that are found on the Second xml : 现在,我正在做一个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));

This works fine, and when I Iterate over this IEnumerable it prints the Name="Comment" and so on. 这可以正常工作,并且当我遍历此IEnumerable它将打印Name="Comment" ,依此类推。

My problem is that it only works for the first XElement. 我的问题是,它仅适用于第一个XElement. So, going back to the first Xml . 因此,回到第first Xml It only prints the Children from the Node with cid = 0x158b . 它仅从cid = 0x158b的节点打印Children cid = 0x158b After its done with that Element it throws the NullReferenceException and does not go to to the one with the cid = 0x13313 . 完成该元素后,它将引发NullReferenceException ,并且不会转到cid = 0x13313 Why is this happening? 为什么会这样呢?

Thanks! 谢谢!

Yes. 是。 As @Selman22 alarmly inferred, my problem was in Element.Elements() . 正如@Selman22意外推断的那样,我的问题出在Element.Elements() Also I added a better Where clause to check if the attribute existed. 我还添加了一个更好的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