Remove XML Node

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William

    Remove XML Node

    Have somebody a brilliant idea how to remove a nodelist in a XML
    document? (With C#)

    I have this xml file,I want delete the stock nodelist when an user
    don't have permissions to see stocklist of that location.

    <Respo1>
    <From>Test</From>
    <To>Test</To>
    <MsgType>RESPO1 </MsgType>
    <MsgVer>001</MsgVer>
    <MsgID>17124356 95</MsgID>
    <MsgDtTm>2005-08-18-17.01.47.634000 </MsgDtTm>
    <CustID>13230 0</CustID>
    <QueryID>I10239 32000030</QueryID>
    <Cux>EUR</Cux>
    <Articles>
    <A>
    <Lin>
    </Lin>
    <S>FVR1039</S>
    <Stock>
    <LocID>01</LocID>
    <Qty>135,00</Qty>
    <NetPr>50,00</NetPr>
    </Stock>
    <Stock>
    <LocID>05</LocID>
    <Qty>1,00</Qty>
    <NetPr>50,00</NetPr>
    </Stock>
    </A>
    </Articles>
    </Respo1>

    example: The user is not authorize to see the information where LocID
    is 01. How can I delete the stock nodelist where LocID is 01.

    Thanks for your advice
    Regards,

    William Mayvis

  • Martin Honnen

    #2
    Re: Remove XML Node



    William wrote:
    [color=blue]
    > Have somebody a brilliant idea how to remove a nodelist in a XML
    > document? (With C#)[/color]

    If you have an XmlDocument and nodes in that document then to remove a
    single node all you need to do is
    node.ParentNode .RemoveChild(no de);
    With node lists you need to be careful as DOM nodes lists are live
    collections thus if you have one and remove a node from the document
    then it is removed from the node list as well.
    So code alike
    while (nodeList.Count > 0) {
    nodeList[0].ParentNode.Rem oveChild(nodeLi st[0]);
    }
    should do.

    --

    Martin Honnen --- MVP XML

    Comment

    • Rami Farhat

      #3
      RE: Remove XML Node

      Hi,
      [color=blue]
      >How can I delete the stock nodelist where LocID is 01[/color]

      Inaddition to Martin's reply , you can use 'an xpath query to get to the
      node or nodelists with LocID=01 or whatever id you want and then delete them
      as follows

      //for node list//
      System.Xml.XmlN odeList nodeList =
      doc.SelectNodes ("/Respo1/Articles/A/Stock[LocID=01]");

      //for single node//
      System.Xml.XmlN ode node =
      doc.SelectSingl eNode("/Respo1/Articles/A/Stock[LocID=01]");
      node.ParentNode .RemoveChild(no de);

      Regards,
      Rami Farhat

      "William" wrote:
      [color=blue]
      > Have somebody a brilliant idea how to remove a nodelist in a XML
      > document? (With C#)
      >
      > I have this xml file,I want delete the stock nodelist when an user
      > don't have permissions to see stocklist of that location.
      >
      > <Respo1>
      > <From>Test</From>
      > <To>Test</To>
      > <MsgType>RESPO1 </MsgType>
      > <MsgVer>001</MsgVer>
      > <MsgID>17124356 95</MsgID>
      > <MsgDtTm>2005-08-18-17.01.47.634000 </MsgDtTm>
      > <CustID>13230 0</CustID>
      > <QueryID>I10239 32000030</QueryID>
      > <Cux>EUR</Cux>
      > <Articles>
      > <A>
      > <Lin>
      > </Lin>
      > <S>FVR1039</S>
      > <Stock>
      > <LocID>01</LocID>
      > <Qty>135,00</Qty>
      > <NetPr>50,00</NetPr>
      > </Stock>
      > <Stock>
      > <LocID>05</LocID>
      > <Qty>1,00</Qty>
      > <NetPr>50,00</NetPr>
      > </Stock>
      > </A>
      > </Articles>
      > </Respo1>
      >
      > example: The user is not authorize to see the information where LocID
      > is 01. How can I delete the stock nodelist where LocID is 01.
      >
      > Thanks for your advice
      > Regards,
      >
      > William Mayvis
      >
      >[/color]

      Comment

      Working...