How to read xml file in c# with specific attribute

106 Views Asked by At
I have an xml file with attribute
 xmlns="http://www.reservwire.com/namespace/WebServices/Xml">

When I remove this attribute, then I can read each tag. If I don't remove attribute I receive error message "Object refrence not set to instance of object"

My C# code:

XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("~/HotelSearchCriteria/PrepareBooking.xml"));
string CommmitLevel = xml.DocumentElement                    
                         .SelectSingleNode("/BookingCreate/CommitLevel")
                         .InnerText;

My XML:

<BookingCreate xmlns="http://www.reservwire.com/namespace/WebServices/Xml">
  <Authority>
    <Org>danco</Org>
    <User>xmltest</User>
    <Password>xmltest</Password>
    <Language>en</Language>
    <Currency>EUR</Currency>
    <TestDebug>false</TestDebug>
    <Version>1.26</Version>
  </Authority>
  <QuoteId>17081233-3</QuoteId>
  <HotelStayDetails>
    <Room>
      <Guests>
        <Adult title="Mr" first="djkvb" last="jkj" />
        <Adult title="Mr" first="jfs" last="kjdjs" />
      </Guests>
    </Room>
  </HotelStayDetails>
  <HotelSearchCriteria>
    <AvailabilityStatus>allocation</AvailabilityStatus>
    <DetailLevel>basic</DetailLevel>
  </HotelSearchCriteria>
  <CommitLevel>prepare</CommitLevel>
</BookingCreate>

What to do to read xml with xmlns attribute? It is necessary for me to have xmlns attribute.

2

There are 2 best solutions below

0
On

You need to specify the namespace, by using a namespace manager. This should work

    XmlDocument bookingXml = new XmlDocument();
    bookingXml.Load("PrepareBooking.xml");
    XmlNamespaceManager ns = new XmlNamespaceManager(bookingXml.NameTable);
    ns.AddNamespace("booking", "http://www.reservwire.com/namespace/WebServices/Xml");
    var commitLevel = bookingXml.SelectSingleNode("//booking:BookingCreate//booking:CommitLevel", ns).InnerText;
0
On

Using xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement bookingCreate = doc.Descendants().Where(x => x.Name.LocalName == "BookingCreate").FirstOrDefault();
            XNamespace ns = bookingCreate.GetDefaultNamespace();

            var results = doc.Descendants(ns + "BookingCreate").Select(x => new {
                org = (string)x.Descendants(ns + "Org").FirstOrDefault(),
                user = (string)x.Descendants(ns + "User").FirstOrDefault(),
                password = (string)x.Descendants(ns + "Password").FirstOrDefault(),
                language = (string)x.Descendants(ns + "Language").FirstOrDefault(),
                currency = (string)x.Descendants(ns + "Currency").FirstOrDefault(),
                testDebug = (Boolean)x.Descendants(ns + "TestDebug").FirstOrDefault(),
                version = (string)x.Descendants(ns + "Version").FirstOrDefault(),
                quoteId = (string)x.Element(ns + "QuoteId"),
                guests = x.Descendants(ns + "Adult").Select(y => new {
                    title = (string)y.Attribute("title"),
                    first = (string)y.Attribute("first"),
                    last = (string)y.Attribute("last")
                }).ToList(),
                availability = (string)x.Descendants(ns + "AvailabilityStatus").FirstOrDefault(),
                detailLevel = (string)x.Descendants(ns + "DetailLevel").FirstOrDefault()
            }).FirstOrDefault();
        }
    }
}