How do I read the contents of an XML element using C#? [duplicate]
Possible Duplicate:
Best practices to parse xml fi开发者_运维百科les?
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?
XDocument xdoc = XDocument.Load("file.xml"));
var elm = from element in xdoc.Descendants("element")
select new {
attribute = element.Attribute("attribute").Value,
};
...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
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
精彩评论