[visual-studio]从 Windows Phone 8 特定属性在 C# 中的阅读 xml 收据存储

发布时间: 2014/3/1 3:10:18
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想创建的 XML 读取器获取"Id"值从收到 windows 8 手机存储中。

<?xml version="1.0"?><Receipt Version="1.0" CertificateId="FB3D3A6455095D2C4A841AA8B8E20661B10A6112" xmlns="http://schemas.microsoft.com/windows/2012/store/receipt"><ProductReceipt PurchasePrice="1.0" PurchaseDate="8:39:19 PM" Id="5d1d90b2-2608-499d-9fcb-f08352aa4842" AppId="bcece01c-42df-4f4c-8a9b-18bb5c49bc73" ProductId="package4" ProductType="Consumable" PublisherUserId="00000000000000000000000000000000000000000000" PublisherDeviceId="00000000-0000-0000-0000-000000000000" MicrosoftProductId="ca65ff27-5472-48a1-95ee-a49591390855" /><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /><DigestValue>whocares</DigestValue></Reference></SignedInfo><SignatureValue>whocares</SignatureValue></Signature></Receipt>

我只已经能够得到 '1.0' 作为输出使用 MoveToFirstAttribute 时返回。我试过 MoveToAttribute("Id") 。他们就是我想要读的属性。

StringBuilder output = new StringBuilder();

            using (XmlReader reader = XmlReader.Create(new StringReader(receipt)))
            {
                reader.ReadToFollowing("ProductReceipt");
                reader.MoveToFirstAttribute();
                output.AppendLine(reader.Value);
            }

            return output.ToString();
        }

解决方法 1:

请参阅是否这澄清发生了什么事。它将它加载到一个文档,而不是读卡器,允许您为某件事问。在这种情况下,你在找什么是 ProductReceipt 元素的属性。

System.Xml.XmlDocument r = new System.Xml.XmlDocument();
r.LoadXml(receipt);
foreach (System.Xml.XmlNode n in r.GetElementsByTagName("ProductReceipt"))
{
    System.Diagnostics.Debug.WriteLine(string.Format("Node: {0}", n.Name));
    foreach (System.Xml.XmlAttribute a in n.Attributes)
    {
        System.Diagnostics.Debug.WriteLine(string.Format("\tAttribute {0}: {1}", a.Name, a.Value));
    }
}
赞助商