如何使用特定属性读取c#中的xml文件

时间:2017-12-06 06:33:34

标签: c# xml readxml

我有一个带属性的xml文件
 xmlns="http://www.reservwire.com/namespace/WebServices/Xml">

当我删除此属性时,我可以读取每个标记。如果我没有删除属性,则会收到错误消息"对象引用未设置为对象的实例"

我的C#代码:

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

我的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>

如何使用xmlns属性读取xml?我必须拥有xmlns属性。

2 个答案:

答案 0 :(得分:0)

您需要使用命名空间管理器指定命名空间。这应该工作

    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;

答案 1 :(得分:0)

使用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();
        }
    }
}