Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how we can set value for xml element using XmlDocument class

Tags:

c#

xml

Is it possible to set value dynamically for any XML element using the XmlDocument class? Suppose my XML is

    <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    <soapenv:Body>
        <v9:ProcessShipmentReply xmlns:v9="http://fedex.com/ws/ship/v9">
            <v9:HighestSeverity xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">SUCCESS</v9:HighestSeverity>
            <v9:Notifications xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <v9:Severity>SUCCESS</v9:Severity>
                <v9:Source>ship</v9:Source>
                <v9:Code>0000</v9:Code>
                <v9:Message>Success</v9:Message>
                <v9:LocalizedMessage>Success</v9:LocalizedMessage>
            </v9:Notifications>
            <v9:CompletedShipmentDetail>
                <v9:CompletedPackageDetails xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                    <v9:SequenceNumber>1</v9:SequenceNumber>
                    <v9:TrackingIds>
                        <v9:TrackingIdType>GROUND</v9:TrackingIdType>
                        <v9:TrackingNumber>634649515000016</v9:TrackingNumber>
                    </v9:TrackingIds>
                    <v9:Barcodes>
                        <v9:BinaryBarcodes>
                            <v9:Type>COMMON_2D</v9:Type>
                            <v9:Value>Wyk+HjAxHTAyMDI3ODAdODQwHTEzNx02MzQ2NDk1</v9:Value>
                        </v9:BinaryBarcodes>
                        <v9:StringBarcodes>
                            <v9:Type>GROUND</v9:Type>
                            <v9:Value>9612137634649515000016</v9:Value>
                        </v9:StringBarcodes>
                    </v9:Barcodes>
                    <v9:Label>
                        <v9:Type>OUTBOUND_LABEL</v9:Type>
                        <v9:ShippingDocumentDisposition>RETURNED</v9:ShippingDocumentDisposition>
                        <v9:Resolution>200</v9:Resolution>
                        <v9:CopiesToPrint>1</v9:CopiesToPrint>
                        <v9:Parts>
                            <v9:DocumentPartSequenceNumber>1</v9:DocumentPartSequenceNumber>
                            <v9:Image>iVBORw0KGgoAAAANSUhEUgAAAyAAAASwAQAAAAAryhMIAAAagEl</v9:Image>
                        </v9:Parts>
                    </v9:Label>
                </v9:CompletedPackageDetails>
            </v9:CompletedShipmentDetail>
        </v9:ProcessShipmentReply>
    </soapenv:Body>

How could I set value for the below element like

<v9:Severity>SUCCESS</v9:Severity>
<v9:Source>ship</v9:Source>

I know how to extract data from XML and I think it is also possible to set value for the XML element using XMLDocument class. Looking for guidance.

like image 393
Mou Avatar asked Jul 11 '11 13:07

Mou


People also ask

How do you change a value in XML?

A simple technique for changing the value of a node is to use node. Value = "new value"; . The following table lists the node types that this single line of code works on and exactly what data for that node type is changed. The value of the attribute.

How do I use XmlDocument?

Using XmlDocumentA Load method loads XML data from a string, stream, TextReader or XmlReader. LoadXml method loads XML document from a specified string. Another useful method of this class is Save. Using Save method you can write XML data to a string, stream, TextWriter or XmlWriter.

What is XmlDocument?

The XmlDocument class is an in-memory representation of an XML document. It implements the W3C XML Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. DOM stands for document object model. To read more about it, see XML Document Object Model (DOM).

What is the difference between XDocument and XmlDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.


1 Answers

If you know how to select a value, then you probably know how to update one too.

XmlDocument doc = new XmlDocument();
doc.Load(...);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("v9", "http://fedex.com/ws/ship/v9");

XmlNode severityNode = doc.SelectSingleNode("//v9:Severity", nsMgr);
severityNode.innerText = "FAILURE";

The important thing to know is that the <v9:Severity> node has an inner text() node, so in the example above you can't use the Node.Value property. To do that you would do something like this instead:

XmlNode severityTextNode = doc.SelectSingleNode("//v9:Severity/text()", nsMgr);
severityTextNode.Value = "FAILURE";

Note the subtle differences.

like image 200
samjudson Avatar answered Sep 19 '22 15:09

samjudson