简体   繁体   中英

manipulation of xml using c#

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>

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");

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

//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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM