简体   繁体   中英

Parse three specific elements from an XML snippet in C# 2.0

How could parse the value of few tag from my XML using C# 2.0?

I want to parse the tag and their value like

1) <v9:Severity>SUCCESS</v9:Severity>

2) <v9:TrackingNumber>634649515000016</v9:TrackingNumber>

3) <v9:Image>iVBORw0KGgoAAAANSUhEUgAAAyAAAASwAQAAAAAryhMIAAAagEl</v9:Image>

How to get the value of those above elements programmatically with C# 2.0?

<?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>
</soapenv:Envelope>

Since you said that you use c# 2.0 (and, thus, cannot use LINQ-to-XML), the easiest way to just find single values out of your XML would be to use XPath:

Since your XML contains namespaces <v9:...> , the issue gets a bit more complicated: You need to initialize an XmlNamespaceManager and pass it to the XPathNavigator . Here is a blog post that explains this issue in detail; an example can also be found at the XmlNode.SelectNodes MSDN page (see link above).

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