Reputation: 5800
i have xml like
<?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>
I want to have <ns2:Amount>
tag value
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;
It gives me error object reference not set to an instance
Upvotes: 1
Views: 141
Reputation: 20058
Just mentioning that correct xml should look something like:
<?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>
This should result in Poco
[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; }
}
In that case
GetBooking booking;
if (xmlstring.Deserialize(out booking))
{
decimal value = booking.Reservation.Extensions.ReservationExt.ExtPayTxInfo.Amount;
//do something with the value
}
ref : deserialize
Upvotes: 1
Reputation: 7508
The problem is that the ns2 + "Amount"
element cannot be found. Therefore, it returns null
and you cannot access the Value
property of a null
object. Since you load the entire XML inside your elem
object, it will "represent" the whole XML, i.e. starting from the GetBooking
element. This element does not have a direct child called Amount
, therefore asking it to return that element will result in a null
object. Using the Descendants
method, the element is searched in the entire sub-tree of the XML and not on the first level.
var targets = elem.Descendants(ns2 + "Amount").ToList();
var value = "";
if (targets.Count > 0)
value = targets[0].Value;
Upvotes: 1