XML Parsing - Removing nodes and keeping the overall structure

179 views Asked by At

so I have a massive XML file that contains a structure similar to this one:

<Item ItemId=";ResTVersion" ItemType="0" PsrId="245" Leaf="false">
    <Disp Icon="Str" Expand="true" Disp="true" LocTbl="false" Order="13352" />
    <Modified By="sachink" DateTime="2008-12-16T19:02:35Z" />
    <PsrProps>
      <Str Name="Kii" Val="yyyyyyyyyyyyy" />
    </PsrProps>
    <Item ItemId=";ResTFileVersion" ItemType="0;ResT" PsrId="245" InstFlg="true" Leaf="true">
      <Str Cat="Text" UsrLk="true">
        <Val><![CDATA[ttttttttt]]></Val>
        <Tgt Cat="Text" Orig="New">
          <Val><![CDATA[ttttttttt]]></Val>
        </Tgt>
      </Str>
      <Disp Icon="Str" Order="13353" />
      <Modified By="sachink" DateTime="2008-12-16T19:02:35Z" />
      <Cmts>
        <Cmt Name="Dev"><![CDATA[{Locked}]]></Cmt>
      </Cmts>
    </Item>
    <Item ItemId=";ResTLanguageTag" ItemType="0;ResT" PsrId="245" InstFlg="true" Leaf="true">
      <Str Cat="Text" UsrLk="true">
        <Val><![CDATA[en-US]]></Val>
        <Tgt Cat="Text" Orig="New">
          <Val><![CDATA[en-US]]></Val>
        </Tgt>
      </Str>
      <Disp Icon="Str" Order="13354" />
      <Modified By="sachink" DateTime="2008-12-16T19:02:35Z" />
      <Cmts>
        <Cmt Name="Dev"><![CDATA[=.ABVUDHUIDSHFUIDSHFUISHDFUIDSH iusdhfUIHAs]]></Cmt>
      </Cmts>
    </Item>
  </Item>

I have several item ids and I want to create a new xml that respects the old structure. I use this code to retrieve the nodes that I want, and then create the new XML.

XmlNodeList nodes = originalXML.SelectNodes("//*[contains(@ItemId,'" + id + "')]");

So what I want is to remove some nodes but I only have the ids of the ones I want to keep. The problem is how do you keep the outer structure of an xml, when you use the selectnodes function, to get the inner nodes?

Thanks!

2

There are 2 answers

0
fejesjoco On

I would go the opposite way: remove what you don't need. It's hard to build an XmlDocument from scratch (takes a lot of coding).

0
void On

I think you will be better off removing the nodes you don't want from the structure you already have.

XmlNodeList nodes = originalXML.SelectNodes("/*[not(contains(@test,'test'))]")
foreach(XmlNode node in nodes)
  originalXML.RemoveChild(node);

should work.

If you need to preserve your original structure you can originalXML.Clone() it.

Sidenote: You might want to look into System.Xml.Linq.XDocument and System.Xml.Linq.XElement I find those a lot easier to use.