2

I want parse xml by looping through it. I referred this but I am not able to parse it using the Load function since it expects an URI parameter and not a string and so does LINQ to XML....Can anyone help me out ?

Community
  • 1
  • 1
Vishal
  • 12,133
  • 17
  • 82
  • 128

4 Answers4

4

XmlDocument has a Load method which takes a filename, but also a LoadXml method which takes a string:

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx

Similarly, XDocument has a Load method which takes a filename, or a Parse method which takes a string:

http://msdn.microsoft.com/en-us/library/bb345532.aspx

Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
0

Similar to XDocument, we can use XElement which has Parse method to parse a xmlString.

See this code:

        string xmlString = @"<poi><city>stockholm</city><country>sweden</country><gpoint><lat>51.1</lat><lng>67.98</lng></gpoint></poi>";
        try
        {
            XElement x = XElement.Parse(xmlString);
            var latLng = x.Element("gpoint");

            Console.WriteLine(latLng.Element("lat").Value);
            Console.WriteLine(latLng.Element("lng").Value);
        }
        catch
        {
        }

Hope this helps.

Mohamed Alikhan
  • 1,315
  • 11
  • 14
0
string recentFileName = Path.Combine(folderPath, filexml);
XDocument xDoc = XDocument.Load(recentFileName);
Martin Ongtangco
  • 22,657
  • 16
  • 58
  • 84
0
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<root>" +
                    "<elem>some text<child/>more text</elem>" +
                    "</root>");
Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108