-7

Possible Duplicate:
Best practices to parse xml files?

I want to be able to search an XML document for a specific element, then take all that is in the element and store each new line within the element into a string array using C#. How would I do that?

Community
  • 1
  • 1
Alper
  • 1
  • 12
  • 39
  • 78

3 Answers3

2
XDocument xdoc = XDocument.Load("file.xml"));
var elm = from element in xdoc.Descendants("element")
           select new { 
               attribute = element.Attribute("attribute").Value,
           };
Erre Efe
  • 15,387
  • 10
  • 45
  • 77
  • How do I access XDocument? It says "The type or namespace name 'XDocument' could not be found (are you missing a using directive or an assembly reference?)" How can I fix that error? – Alper Jul 29 '11 at 22:37
  • @Jacob: using system.xml.linq it's linq to xml. – Erre Efe Jul 29 '11 at 22:38
1

...alternatively, you could use the pre-existing settings framework in Visual Studio. It won't suit a custom XML file, but if the idea is to read/write application settings from/to an XML file, it does most of the hard work for you. It will select a suitable location for such a file, auto-generate code such that it's very easy to interact with settings and it abstracts away the file IO side of things. Definitely worth a peek to check that you're not re-inventing it:

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

spender
  • 117,338
  • 33
  • 229
  • 351
1

Here's some brain compiled pseudo code

XmlDocument doc = new XmlDocument();
doc.Load("myDoc.xml");
XmlNodeList list = doc.GetElementsByTagName("My Element");
foreach (XmlNode node in list)
    //process node
Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88