locked
Deleting a Node RRS feed

  • Question

  • User-541003552 posted

    Hi All,

    I have an xml file with the structure below. How can i delete an Item Node with all its subnodes where the ItemSeqNo is equal to a specofic number.

    <?xml version="1.0" encoding="utf-8"?>
    <FileHeader xmlns="urn:schemas-coded-com:ECGIXC:NXF:FileStructure:040001" VersionNumber="020001" TestFileIndicator="P" CreationDate="03072014" CreationTime="182158" FileID="11">
    <CashLetterHeader CLID="03071400017" CLCreationDate="03072014" CLCreationTime="182158">
    <BundleHeader BundleID="03071400018" BCreationDate="03072014" BCreationTime="182158">
    
    <Item ItemSeqNo="10001">
    <AddendA>
    <ImageViewDetail viewside="Front">
    <ImageViewData  imagefile=""/>
    </ImageViewDetail>
    <ImageViewDetail viewside="Back">
    <ImageViewData imagefile="" />
    </ImageViewDetail>
    </AddendA>
    </Item>
    
    <Item ItemSeqNo="10002">
    <AddendA>
    <ImageViewDetail viewside="Front">
    <ImageViewData  imagefile=""/>
    </ImageViewDetail>
    <ImageViewDetail viewside="Back">
    <ImageViewData imagefile="" />
    </ImageViewDetail>
    </AddendA>
    </Item>
    </BundleHeader>
    </CashLetterHeader>
    </FileHeader>
    </xml>

    Any help and advice.

    Thanks

    Tuesday, December 16, 2014 5:16 PM

Answers

  • User281315223 posted

    You could read your file into an XmlDocument object and then iterate through your specific nodes until you find one that matches what you want to delete and then simply delete it :

    // Node values to delete (example to handle multiple items)
    var itemsToDelete = new []{"10001"};
    
    // Create an XML Document
    XmlDocument doc = new XmlDocument();
    // Read your XML file into the document
    doc.Load("Your XML or XML File Path");
    
    // Find your items
    XmlNodeList nodes = doc.GetElementsByTagName("Item");
    // Loop through them to see if any should be deleted
    for(int i = 0; i < nodes.Count; i++)
    {
           // Check if your item to delete
           if(itemsToDelete.Contains(Convert.ToString(nodes[i].Attributes["ItemSeqNo"].Value)))
           {
                   // Delete the node from the parent
                   nodes[i].ParentNode.RemoveChild(nodes[i]);
           }
    }
    
    // Save the document
    doc.Save("Your XML Path");

    After running this code, your XML file would look like :

    <?xml version="1.0" encoding="utf-8"?>
    <FileHeader xmlns="urn:schemas-coded-com:ECGIXC:NXF:FileStructure:040001" VersionNumber="020001" TestFileIndicator="P" CreationDate="03072014" CreationTime="182158" FileID="11">
      <CashLetterHeader CLID="03071400017" CLCreationDate="03072014" CLCreationTime="182158">
        <BundleHeader BundleID="03071400018" BCreationDate="03072014" BCreationTime="182158">
          <Item ItemSeqNo="10002">
            <AddendA>
              <ImageViewDetail viewside="Front">
                <ImageViewData imagefile="" />
              </ImageViewDetail>
              <ImageViewDetail viewside="Back">
                <ImageViewData imagefile="" />
              </ImageViewDetail>
            </AddendA>
          </Item>
        </BundleHeader>
      </CashLetterHeader>
    </FileHeader>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 16, 2014 5:42 PM