在 C# 2.0 中解析 XML 片段中的三个特定元素

发布于 2024-11-18 10:39:32 字数 3431 浏览 2 评论 0原文

如何使用 C# 2.0 解析 XML 中几个标签的值?

我想解析标签及其值,例如

1) SUCCESS

2) 6346495150000163

iVBORw0KGgoAAAANSUhEUgAAAyAAAASwAQAAAAAryhMIAAAagEl

如何使用 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2024-11-25 10:39:32

既然您说您使用 c# 2.0(因此不能使用 LINQ-to-XML),那么从 XML 中查找单个值的最简单方法就是使用 XPath:

由于您的 XML 包含命名空间 ,因此问题变得更加复杂:您需要初始化 XmlNamespaceManager 并将其传递给 >XPathNavigator。这是一篇博客文章,详细解释了这个问题;还可以在 XmlNode.SelectNodes MSDN 页面上找到示例(请参阅上面的链接)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文