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 ?
Asked
Active
Viewed 2,677 times
4 Answers
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:

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
-
2instead of using XmlDocument, use XDocument. xml-to-linq will be similar. – Martin Ongtangco Jul 30 '10 at 01:26
0
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>" +
"<elem>some text<child/>more text</elem>" +
"</root>");

Lukasz Madon
- 14,664
- 14
- 64
- 108