我正在尝试使用C#
阅读以下XML <GetAssetWarrantyResponse xmlns="http://tempuri.org/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<GetAssetWarrantyResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset">
<a:Faults />
<a:Response>
<a:DellAsset>
<a:AssetParts i:nil="true" />
<a:CountryLookupCode>XXXX</a:CountryLookupCode>
<a:CustomerNumber>XXXXXX</a:CustomerNumber>
<a:IsDuplicate>false</a:IsDuplicate>
<a:ItemClassCode>XXX</a:ItemClassCode>
<a:LocalChannel>XXXX</a:LocalChannel>
<a:MachineDescription>XXXXXXX</a:MachineDescription>
<a:OrderNumber>XXXXXX</a:OrderNumber>
<a:ParentServiceTag i:nil="true" />
<a:ServiceTag>XXXXXX</a:ServiceTag>
<a:ShipDate>2010-04-12T19:00:00</a:ShipDate>
<a:Warranties>
<a:Warranty>
<a:EndDate>2011-04-13T18:59:59</a:EndDate>
<a:EntitlementType>INITIAL</a:EntitlementType>
<a:ItemNumber>709-10398</a:ItemNumber>
<a:ServiceLevelCode>CB</a:ServiceLevelCode>
<a:ServiceLevelDescription>Collect and Return Initial with Dates</a:ServiceLevelDescription>
<a:ServiceLevelGroup>5</a:ServiceLevelGroup>
<a:ServiceProvider i:nil="true" />
<a:StartDate>2010-04-12T19:00:00</a:StartDate>
</a:Warranty>
</a:Warranties>
</a:DellAsset>
</a:Response>
</GetAssetWarrantyResult>
</GetAssetWarrantyResponse>
我认为我的命名空间存在问题,到目前为止我尝试过的任何问题都没有用。
到目前为止我已经尝试了
XNamespace aw = "a"
XNamespace aw = "a:"
XNamespace aw = "http://schemas.xmlsoap.org/soap/envelope/"
以及我目前无法想起的其他一些
private void btDellWaranty_Click(object sender, EventArgs e)
{
string serviceTag = tbDellServiceTag.Text;
string uri= https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=XXXX&apikey=XXXX";
XDocument Doc = XDocument.Load(uri);
XNamespace aw = "a";
string result = doc.from u in Doc.Descendants(aw + "DellAsset") select (string)u.Element(aw + "MachineDescription").Value;
XElement result = Doc.Root.Element("GetAssetWarrantyResponse");
}
有什么想法吗?
注意:我正确地将tbDellServiceTag.Text
传递给了uri
,所以这不是问题。
答案 0 :(得分:1)
前缀的名称空间(例如a:DellAsset
具有前缀a
)由元素的xmlns:a="..."
或包含该名称空间声明的第一个祖先元素表示 - {{1在这种情况下。
因此,您的命名空间是:
GetAssetWarrantyResult
答案 1 :(得分:1)
尝试像这样的xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication6
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement dellAsset = doc.Descendants().Where(x => x.Name.LocalName == "DellAsset").FirstOrDefault();
XNamespace ns = dellAsset.Name.Namespace;
string customerNumber = (string)dellAsset.Element(ns + "CustomerNumber");
}
}
}