0

If I want to add, update or delete node in the xml using c#, how can it be done? My xml is shown below. I dont want transactionID node. I want to add a node called <Transformation>XML</Transformation> after corelationID node.

<?xml version="1.0" ?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
    <EnvelopeVersion>2.0</EnvelopeVersion>
    <Header>
        <MessageDetails>
            <Class>HMRC-VAT-DEC</Class>
            <Qualifier>poll</Qualifier>
            <Function>submit</Function>
            <TransactionID />
            <CorrelationID>1B93D48C02D740C6B79DE68A27F3ADE5</CorrelationID>
            <ResponseEndPoint PollInterval="10">https://secure.dev.gateway.gov.uk/poll</ResponseEndPoint>
            <GatewayTimestamp>2011-04-05T07:41:43.018</GatewayTimestamp>
        </MessageDetails>
        <SenderDetails />
    </Header>
    <GovTalkDetails>
        <Keys />
    </GovTalkDetails>
    <Body />
</GovTalkMessage>

3 Answers 3

2

The easiest thing to use would be LINQ to XML. For example:

XDocument doc = XDocument.Load("file.xml");
XNamespace ns = "http://www.govtalk.gov.uk/CM/envelope";

// Remove TransationID
XElement transactionElement = doc.Descendants(ns + "TransactionID").Single();
transactionElement.Remove();

// Add XML:
XElement correlationElement = doc.Descendants(ns + "CorrelectionID").Single();
XElement newElement = new XElement(ns + "XML");
correlationElement.AddAfterSelf(newElement);

// Save back
doc.Save("new-file.xml");
Sign up to request clarification or add additional context in comments.

2 Comments

I am using Framework 2.0. So its not possible to use Linq. Is there any other method??
@shakul: Yes, you can use XmlDocument and XmlElement. But the code will be much uglier :(
0
//Load the XML
    XmlDocument documentXML = new XmlDocument();
    documentXML.Load(Server.MapPath("AddDeleteUpdate.xml"));

    XmlNamespaceManager xmlns = new XmlNamespaceManager(documentXML.NameTable);
    xmlns.AddNamespace("bk", "http://www.govtalk.gov.uk/CM/envelope");

    //Identify the parent node i.e <MessageDetails>
    XmlNode nodeMessage = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails", xmlns);

    //Delete the node.
    XmlNode nodeTransactionID = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails/bk:TransactionID", xmlns);
    nodeMessage.RemoveChild(nodeTransactionID);

    //Create the new XML noded to be added.
    XmlNode controlAttrNode = null;
    controlAttrNode = documentXML.CreateElement("Transformation");
    controlAttrNode.InnerText = "XML";
    controlAttrNode.Attributes.RemoveAll();

    //Get the node object to where it need to be added.
    XmlNode nodeCorrelation = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails/bk:CorrelationID", xmlns);
    //Insert the node after.
    nodeMessage.InsertAfter(controlAttrNode, nodeCorrelation);

    documentXML.Save(Server.MapPath("AddDeleteUpdate.xml"));

Comments

0

You'll need

XMLNode.InsertAfter(newChildNode,referenceChildNode)

This should kickstart you:

http://msdn.microsoft.com/en-US/library/system.xml.xmlnode.insertafter%28v=VS.80%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.