[英]XML Parsing from the string?
我在我的應用中使用In App Purchase。 這是成功購買后收到的收據:
<?xml version="1.0"?>
<Receipt Version="1.0" CertificateId="xxxyyy" xmlns="http://schemas.microsoft.com/windows/2012/store/receipt">
<ProductReceipt PurchasePrice="$9.99" PurchaseDate="2014-11-18T08:04:37.572Z" ExpirationDate="9999-12-31T00:00:00Z" ProductId="product_id" ProductType="Consumable" AppId="xxx-yyy" Id="xxx-yyy" PublisherDeviceId="xxx-yyy" PublisherUserId="" MicrosoftProductId="xxx-yyy" MicrosoftAppId="xxx-yyy" />
</Receipt>
//存儲為字符串“ receipt”
我需要將其顯示給用戶,但出現錯誤。 這是我的代碼:
var xDoc = XDocument.Parse(receipt).Root.Element("ProductReceipt"); //receipt id the string
string Price = xDoc.Attribute("PurchasePrice").Value;
“ xDoc”始終為空。 由於它為空,因此我在獲取價格方面遇到了例外。
你調用的對象是空的。
請幫助我如何從該xml字符串獲取“ PurchasePrice”!
您將需要使用命名空間 :
XNamespace ns = "http://schemas.microsoft.com/windows/2012/store/receipt";
var xDoc = XDocument
.Parse(receipt)
.Root
.Element(ns + "ProductReceipt"); //Use namespace ProductReceipt is in
請記住,如果您的xml包含xmlns
標記,則該節點及其子節點是該名稱空間的一部分。 這是為了防止元素名稱沖突。
如果您想了解更多關於Wikipedia的文章,那么它是一個很好的起點: http : //en.wikipedia.org/wiki/XML_namespace
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.