复杂的xml读取某些元素

时间:2017-11-17 23:28:45

标签: c# xml vb.net

我们在字符串中收到这个xml,为了我的死,我似乎无法获得任何数据。我们只需要几个值,但它们全面深入。在下面的xml中,我们希望获得

ShippingService>率

标签>&FileContents GT;内容

TrackingId

我尝试过xmldocument,xdocument,linq,似乎得到了任何信息。我们在其他代码中使用了xdocument和linq并且运行良好,因为有多个seqments要循环,它更简单但不在这个xml上。

<?xml version="1.0"?>
<CreateShipmentResponse xmlns="https://www.example.com">
    <CreateShipmentResult>
        <Shipment>
            <OrderId>123456789</OrderId>
            <Weight>
                <Value>8</Value>
                <Unit>oz</Unit>
            </Weight>
            <Label>
                <FileContents>
                    <Contents>fP1R/6hZ8+/lw4W8uIglTthyg2l8mUfM4752G5NEy6hC...</contents>
                    <FileType>image/png</FileType>
                </FileContents>
                <LabelFormat>PNG</LabelFormat>
                <Dimensions>
                <Width>4.0</Width>
                <Length>6.0</Length>
                <Unit>inches</Unit>
                </Dimensions>
            </Label>
            <ShippingService>
                <CarrierName>USPS</CarrierName>
                <ShippingServiceOptions>
                    <LabelFormat>PNG</LabelFormat>
                    <CarrierWillPickUp>false</CarrierWillPickUp>
                    <DeclaredValue>
                    <Amount>0.00</Amount>
                    </DeclaredValue>
                    <DeliveryExperience>DeliveryConfirmationWithoutSignature</DeliveryExperience>
                </ShippingServiceOptions>
                <ShippingServiceId>USPS_PTP_FC</ShippingServiceId>
                <Rate>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>2.77</Amount>
                </Rate>
                <ShippingServiceName>USPS First Class</ShippingServiceName>
            </ShippingService>
            <ShipmentId>a098b57ba5384fa39a32b89674b51714</ShipmentId>
            <TrackingId>9300120111402290737718</TrackingId>
        </Shipment>
    </CreateShipmentResult>
    <ResponseMetadata>
        <RequestId>f9fed16b076a44818486966ee7efb117</RequestId>
    </ResponseMetadata>
</CreateShipmentResponse>

我一定很累,因为我觉得这很简单。谢谢你的帮助

Dim doc As XDocument = XDocument.Parse(strResponseData)
            Dim ns As XNamespace = "https://www.example.com/shipment/2015-06-01"
            Dim shipment As XElement = doc.Descendants(ns + "Shipment").First()

            Shipment.rate = shipment.Element("ShippingService").Element("Rate").Element("Amount").Value
Shipment.rate = shipment.Element(ns + "ShippingService").Element(ns + "Rate").Element(ns + "Amount").Value

我也试过这个并得到同样的错误

Dim doc = XDocument.Load("c:\files\11466778294298624.xml").Descendants(ns + "ShippingService").FirstOrDefault().Element(ns + "Rate")
        Shipment.rate = CType(doc.Element(ns + "Amount"), Decimal)

1 个答案:

答案 0 :(得分:0)

您的示例XML没有定义任何名称空间,但在您正在使用的代码中

  

Dim ns As XNamespace =&#34; https://www.example.com/shipment/2015-06-01&#34;

当您阅读XML时。

删除命名空间,它将起作用

    Dim doc As XDocument = XDocument.Parse(s)
    Dim shipment As XElement = doc.Descendants("Shipment").First()

此部分XML也不正确

<Contents>fP1R/6hZ8+/lw4W8uIglTthyg2l8mUfM4752G5NEy6hC...</contents>

结束标记应为大写C </Contents>