我使用API从服务器提取XML数据,并使用LINQ将结果存储在匿名对象列表中,但是有些数据正在返回" null"我认为这必须归功于XML文档层次结构。见下文 -
<export type="orderexport" date="13/01/2015" time="5:01:18 PM">
<order>
<OrderNumber>149612</OrderNumber>
<CustomerNumber>146538</CustomerNumber>
<Currency>AUD</Currency>
<Locale>en_AU</Locale>
<GrandTotal>1548.98</GrandTotal>
<TotalBeforeTax>1411.58</TotalBeforeTax>
<TotalTax>137.4</TotalTax>
<Addresses>
<BillingAddress>
<Email>XXXXXX@XXXXXXX.com.au</Email>
</BillingAddress>
<ShippingAddress>
<Email>XXXXXX@XXXXXXX.com.au</Email>
</ShippingAddress>
</Addresses>
<LineItems>
<LineItem>...</LineItem>
<LineItemSalesDiscount>...</LineItemSalesDiscount>
<LineItemDiscount>...</LineItemDiscount>
<LineItemShipping>...</LineItemShipping>
<LineItemPayment>...</LineItemPayment>
<LineItemPaymentDiscount>...</LineItemPaymentDiscount>
</LineItems>
<CreationDate>13/01/2015 9:42:59 AM</CreationDate>
<ViewedOn>13/01/2015 9:44:20 AM</ViewedOn>
<cancelledon/>
<readyforshippingon>13/01/2015 1:58:41 PM</readyforshippingon>
<paidon>13/01/2015 9:43:15 AM</paidon>
<ordercomment>
removed
</ordercomment>
</order>
</export>
我可以在第一级提取所有元素,但是我需要包含标记,但它嵌套在另一个级别,当我尝试获取其值时,我得到其余部分,我得到一个空值。
//Create Request
myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
//Get Response
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//Load response stream into XMLReader
myXMLReader = new XmlTextReader(myHttpWebResponse.GetResponseStream());
var xdoc = XDocument.Load(myXMLReader);
var result = from x in xdoc.Root.Elements()
select new
{
OrderNumber = (string)x.Element("OrderNumber"),
CustomerNumber = (string)x.Element("CustomerNumber"),
GrandTotal = (double)x.Element("GrandTotal"),
Email = (string)x.Element("Email")
};
输出:&#34; {OrderNumber =&#34; 149612&#34;,CustomerNumber =&#34; 146538&#34;,GrandTotal = 1548.98,Email = null}&#34 ;
我之前从未阅读过XML文档,但我确信上面的代码是正确的,我确定我只需要更改电子邮件的LINQ表达式行,但我不知道应该是什么。
此致
答案 0 :(得分:1)
由于Email
中存在Addresses\BillingAddress
,您需要提取这样的电子邮件: -
Email = x.Descendants("BillingAddress")
.Select(e => (string)e.Element("Email")).FirstOrDefault()
请注意,我只提取BillingAddress
节点内的电子邮件地址,如果您希望ShippingAddress
节点内存在电子邮件地址,您可以这样做: -
Email = x.Descendants("ShippingAddress")
.Select(e => (string)e.Element("Email")).FirstOrDefault();
答案 1 :(得分:0)
你可以尝试简单的方法。您已经了解了API返回的XML结构,
This link显示了如何做到这一点。