2014-09-11 70 views
1

我对XML喜欢XML命名空间获取XML元素值

<?xml version="1.0" encoding="UTF-8"?> 
<GetBooking Nmbr="0015151001" Identifier="1771C9A911E98" Version="2006.01" Token="11868765"> 
    <Reservation xmlns="http://www.facebook.org/someurl"> 
     <Extensions> 
     <ns2:ReservationExt xmlns="http://www.facebook.org/someurl xmlns:ns2="http://www.google.com/india"> 
      <ns2:ExtPayTxInfo> 
       <ns2:ReferenceID>35775726</ns2:ReferenceID> 
       <ns2:QueryRPH>35775726NI10054145950</ns2:QueryRPH> 
       <ns2:Status>1</ns2:Status> 
       <ns2:Amount>17.85</ns2:Amount> 
       <ns2:Code>9</ns2:Code> 
       <ns2:TxStatus>1</ns2:TxStatus> 
       <ns2:Timestamp>2014-09-10T05:41:45</ns2:Timestamp> 
       <ns2:EndTimestamp>2014-09-10T05:41:45</ns2:EndTimestamp> 
      </ns2:ExtPayTxInfo> 
     </ns2:ReservationExt> 
     </Extensions> 
    </Reservation> 
    <Success xmlns="http://www.facebook.org/someurl" /> 
</GetBooking> 

我想有<ns2:Amount>标记值

string xml = "";// XML Pasted Above 
XNamespace ns1 = "http://www.facebook.org/someurl"; 
XNamespace ns2 = "http://www.google.com/india"; 

var elem = XElement.Parse(xml); 
var value = elem.Element(ns2 + "Amount").Value; 

它给了我错误对象引用未设置为一个实例

回答

1

问题是找不到ns2 + "Amount"元素。因此,它返回null,并且您不能访问null对象的Value属性。由于您在elem对象中加载了整个XML,因此它将“代表”整个XML,即从GetBooking元素开始。此元素没有直接子元素Amount,因此要求它返回该元素将导致null对象。使用Descendants方法,将在XML的整个子树中搜索元素,而不是在第一级搜索。

var targets = elem.Descendants(ns2 + "Amount").ToList(); 
var value = ""; 
if (targets.Count > 0) 
    value = targets[0].Value; 
+0

在参考文献中找不到“Descendant”。 – Shaggy 2014-09-11 07:04:25

+0

@Shaggy,哎呀!纠正了这一点。 – 2014-09-11 07:10:41

+0

为什么两个'..'进入? – Shaggy 2014-09-11 07:18:47

1

刚刚提的是正确的XML应该是这个样子:

<?xml version="1.0" encoding="UTF-8"?> 
<GetBooking Nmbr="0015151001" Identifier="1771C9A911E98" Version="2006.01" Token="11868765" xmlns:f="http://www.w3.org/TR/html4/" xmlns:ns2="http://www.google.com/india"> 
    <f:Reservation> 
     <f:Extensions> 
     <ns2:ReservationExt > 
      <ns2:ExtPayTxInfo> 
       <ns2:ReferenceID>35775726</ns2:ReferenceID> 
       <ns2:QueryRPH>35775726NI10054145950</ns2:QueryRPH> 
       <ns2:Status>1</ns2:Status> 
       <ns2:Amount>17.85</ns2:Amount> 
       <ns2:Code>9</ns2:Code> 
       <ns2:TxStatus>1</ns2:TxStatus> 
       <ns2:Timestamp>2014-09-10T05:41:45</ns2:Timestamp> 
       <ns2:EndTimestamp>2014-09-10T05:41:45</ns2:EndTimestamp> 
      </ns2:ExtPayTxInfo> 
     </ns2:ReservationExt> 
     </f:Extensions> 
    </f:Reservation> 
    <f:Success/> 
</GetBooking> 

这将导致波科

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class GetBooking 
{ 
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.w3.org/TR/html4/")] 
    public Reservation Reservation { get; set; } 
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.w3.org/TR/html4/")] 
    public object Success { get; set; } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public uint Nmbr { get; set; } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Identifier { get; set; } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public decimal Version { get; set; } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public uint Token { get; set; } 
} 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/TR/html4/")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/TR/html4/", IsNullable = false)] 
public partial class Reservation 
{ 
    public ReservationExtensions Extensions { get; set; } 
} 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/TR/html4/")] 
public partial class ReservationExtensions 
{ 
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.google.com/india")] 
    public ReservationExt ReservationExt { get; set; } 
} 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.google.com/india")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.google.com/india", IsNullable = false)] 
public partial class ReservationExt 
{ 
    public ReservationExtExtPayTxInfo ExtPayTxInfo { get; set; } 
} 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.google.com/india")] 
public partial class ReservationExtExtPayTxInfo 
{ 
    public uint ReferenceID { get; set; } 
    public string QueryRPH { get; set; } 
    public byte Status { get; set; } 
    public decimal Amount { get; set; } 
    public byte Code { get; set; } 
    public byte TxStatus { get; set; } 
    public DateTime Timestamp { get; set; } 
    public DateTime EndTimestamp { get; set; } 
} 

在这种情况下

GetBooking booking; 
if (xmlstring.Deserialize(out booking)) 
{ 
    decimal value = booking.Reservation.Extensions.ReservationExt.ExtPayTxInfo.Amount; 
    //do something with the value 
} 

裁判:deserialize

+0

没有得到你写的东西。我将如何拥有'GetBooking'类对象? – Shaggy 2014-09-11 08:02:31

+0

通过** Ctrl + C **代码和**编辑 - 选择性粘贴 - 粘贴为Xml类**(需要+ VS2012 rc2)获得** Poco **。 – Margus 2014-09-11 08:12:32