C# 使用LINQ到XML的问题
我正在尝试使用LINQtoXML解析从Google Checkout收到的通知 答复如下:C# 使用LINQ到XML的问题,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我正在尝试使用LINQtoXML解析从Google Checkout收到的通知 答复如下: <?xml version="1.0" encoding="UTF-8"?> <authorization-amount-notification xmlns="http://checkout.google.com/schema/2" serial-number="153286076708098-00005-6"> <authorization-amount currenc
<?xml version="1.0" encoding="UTF-8"?>
<authorization-amount-notification xmlns="http://checkout.google.com/schema/2" serial-number="153286076708098-00005-6">
<authorization-amount currency="USD">60.0</authorization-amount>
<authorization-expiration-date>2011-07-03T21:27:48.000Z</authorization-expiration-date>
<avs-response>Y</avs-response>
<cvn-response>M</cvn-response>
<timestamp>2011-06-26T21:28:48.741Z</timestamp>
<google-order-number>153286076708098</google-order-number>
<order-summary>
<total-chargeback-amount currency="USD">0.0</total-chargeback-amount>
<google-order-number>153286076708098</google-order-number>
<total-charge-amount currency="USD">0.0</total-charge-amount>
<total-refund-amount currency="USD">0.0</total-refund-amount>
<risk-information>
<ip-address>77.42.229.34</ip-address>
<billing-address>
<address1>somewhere in Beirut</address1>
<address2></address2>
<phone>70892555</phone>
<email>Technical@fisharwe.com</email>
<contact-name>Fisharwe User</contact-name>
<company-name></company-name>
<fax></fax>
<country-code>LB</country-code>
<city>Beirut</city>
<region></region>
<postal-code>1000</postal-code>
</billing-address>
<avs-response>Y</avs-response>
<cvn-response>M</cvn-response>
<eligible-for-protection>true</eligible-for-protection>
<partial-cc-number>1111</partial-cc-number>
<buyer-account-age>18</buyer-account-age>
</risk-information>
<authorization>
<authorization-amount currency="USD">60.0</authorization-amount>
<authorization-expiration-date>2011-07-03T21:27:48.000Z</authorization-expiration-date>
</authorization>
<purchase-date>2011-06-26T21:27:48.000Z</purchase-date>
<archived>false</archived>
<shopping-cart>
<items>
<item>
<item-name>Credits</item-name>
<item-description>Description</item-description>
<unit-price currency="USD">60.0</unit-price>
<quantity>1</quantity>
</item>
</items>
</shopping-cart>
<order-adjustment>
<merchant-codes />
<total-tax currency="USD">0.0</total-tax>
<adjustment-total currency="USD">0.0</adjustment-total>
</order-adjustment>
<promotions />
<buyer-id>975104325298289</buyer-id>
<buyer-marketing-preferences>
<email-allowed>false</email-allowed>
</buyer-marketing-preferences>
<buyer-shipping-address>
<address1>somewhere in Beirut</address1>
<address2></address2>
<phone>70892555</phone>
<email>Technical@fisharwe.com</email>
<contact-name>Fisharwe User</contact-name>
<company-name></company-name>
<fax></fax>
<structured-name>
<first-name>Fisharwe</first-name>
<last-name>User</last-name>
</structured-name>
<country-code>LB</country-code>
<city>Beirut</city>
<region></region>
<postal-code>1000</postal-code>
</buyer-shipping-address>
<order-total currency="USD">60.0</order-total>
<fulfillment-order-state>NEW</fulfillment-order-state>
<financial-order-state>CHARGEABLE</financial-order-state>
</order-summary>
</authorization-amount-notification>
这是我第一次使用LINQtoXML,所以我不确定我的代码到底出了什么问题。但它甚至没有进入if语句的内部。因此,当我将条件替换为:
if(serverResponse.IndexOf(“授权金额通知”)>-1)
最后,我收到错误信息,告诉我没有找到金额
和谷歌编号
有什么建议吗?关于
if(xmlData.Root.Name.LocalName.Equals("new-order-notification")){
....
}
但是您发布的xml似乎与您使用的代码不匹配。。元素不存在您需要将名称空间放入Xml中,并且您可以将元素作为根节点的子元素
您只关注一个元素,因此执行Elements().First()是没有意义的。只需执行Element()
此外,还可以通过将element+名称空间的名称传递给element()方法来匹配元素名称
新订单通知的结束标记在哪里
?可能您对xmlData.Elements(“授权金额”)进行了查询
XML是否使用名称空间?您可能需要将它们添加到查询中:。还可以尝试使用QuickWatch检查元素,看看它们说了什么。实际上,响应XML似乎被截断了。我没有看到authorization amount
或authorization amount notification
。在查询元素时,您必须使用XNamespace
并附加相同的名称,顺便问一下,根在哪里?我修复了我的帖子。我复制了错误的XML块,现在我在帖子中找到了正确的XML块。很抱歉。@DaveShaw:我仍然收到相同的错误序列不包含元素
@Kassem,对不起,我没有运行它。我已经修复了上面的代码。元素位于根节点下,因此您需要执行so Root.Element。@DaveShaw:如何到达不是根的直接子节点的节点?我尝试了username=xmlData.Root.Element(ns+“购物车”).Element(ns+“商家私有数据”).Value代码>但它对我不起作用。。。有什么想法吗?您缺少层次结构中的一个步骤,您需要从根目录中查找每个元素:xmlData.root.element(ns+“订单摘要”).element(ns+“购物车”).Elements(ns+“项目”);如果您为自己获取一份LinqPad的副本,您可以使用.Dump()扩展方法来调试出错的地方。。。当我被我的L2Xml卡住时,我只是向上移动几层,看看那里有什么,哪里出了问题。这就是我用来调试你的问题的工具。
if(xmlData.Root.Name.LocalName.Equals("new-order-notification")){
....
}
var xmlData = XDocument.Parse(xml);
XNamespace ns = "http://checkout.google.com/schema/2";
if (xmlData.Root.Name == ns + "authorization-amount-notification")
{
var amount =
xmlData
.Root
.Element(ns + "authorization-amount")
.Value;
var googleNumber =
xmlData
.Root
.Element(ns + "google-order-number")
.Value;
_checkoutService.ChargeAndShip(googleNumber, amount);
charged = true;
}