我们如何使用XmlDocument类设置xml元素的值

时间:2011-07-11 13:41:10

标签: c# xml

是否可以使用XmlDocument类为任何XML元素动态设置值?假设我的XML是

    <?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>

我如何设置以下元素的值,如

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

我知道如何从XML中提取数据,我认为也可以使用XMLDocument类为XML元素设置值。寻求指导。

2 个答案:

答案 0 :(得分:24)

如果你知道如何选择一个值,那么你可能也知道如何更新一个值。

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

重要的是要知道<v9:Severity>节点有一个内部text()节点,所以在上面的例子中你不能使用Node.Value属性。要做到这一点,你会做这样的事情:

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

请注意细微差别。

答案 1 :(得分:0)

使用XMLDocument类对上级节点执行XPath,并在树上添加2个额外节点。