4

i have a xml response in string..

 string rerricingresponsexml= xmlvalue;

the xmlvalue is

   <?xml version="1.0" encoding="UTF-8"?>
<Repricing>
  <Flights>
    .....
  </Flights>
</Repricing>

Now i want to extract <Flights> to \</Flights>

I tried

        XmlDocument doc = new XmlDocument();
        doc.Load(rerricingresponsexml);

        XmlNode headerNode = doc.SelectSingleNode("RePricing");
        if (headerNode != null)
        {
            string headerNodeXml = headerNode.OuterXml;
        }

But it says Illegal character in xml.. Any help please..

Sasidharan
  • 3,600
  • 3
  • 17
  • 37

3 Answers3

6

Try this:

var xml = XElement.Parse(xmlString);
var flights = xml.DescendantsAndSelf("Flights");

It will return IEnumerable<XElement>. If you want to get it as string you can use this:

string.Concat(flights.Nodes())
Hamlet Hakobyan
  • 32,360
  • 6
  • 50
  • 66
0

Xml is case-sensitive. Use Repricing instead of RePricing. Also you should load xml string with LoadXml method. When you using Load method, document tries to load non-existing file:

doc.LoadXml(rerricingresponsexml);
XmlNode headerNode = doc.SelectSingleNode("Repricing");

Also consider to use Linq to Xml for parsing:

var xdoc = XDocument.Parse(rerricingresponsexml);
var flights = from f in xdoc.Root.Elements("Flights").Elements()
              select new {
                  SearchId = (string)f.Element("SearchId"),
                  Currency = (string)f.Element("Currency"),
                  AdultPrice = (decimal)f.Element("AdultPrice"),
                  // etc
                  Legs = from l in f.Element("LegDetails").Elements()
                         select new {
                             AirLineCode = (string)l.Element("AirLineCode"),
                             FromSector = (string)l.Element("FromSector"),
                             ToSector = (string)l.Element("ToSector")
                             // etc
                         }
              };
Sergey Berezovskiy
  • 224,436
  • 37
  • 411
  • 441
0

Use LinqToXML ad you will be fine with just two lines of code:

XDocument doc = XDocument.Load(rerricingresponsexml);
string flightDetails= doc.Descendants("Flights").SingleOrDefault().ToString();

Hope it helps!

Sunny Sharma
  • 4,346
  • 5
  • 32
  • 69