XML Question
-
I've got a set of xml files which all look similar to this one: Standard Das Standard Layout. default.css standard.jpg Text Version Das Leichtgewicht. textonly.css textonly.jpg The number of item elements varies. What is the easiest way to loop through the xml and extract the individual data items? I had a look at the XmlTextReader but the methods provided in this class seem a bit cumbersum. There must be an easier way... Any help is greatly appreciated. /matthias
-
I've got a set of xml files which all look similar to this one: Standard Das Standard Layout. default.css standard.jpg Text Version Das Leichtgewicht. textonly.css textonly.jpg The number of item elements varies. What is the easiest way to loop through the xml and extract the individual data items? I had a look at the XmlTextReader but the methods provided in this class seem a bit cumbersum. There must be an easier way... Any help is greatly appreciated. /matthias
Try using XPath XmlDocument document = new XmlDocument(); document.Load(filename); XmlNodeList nodes = document.SelectNodes("designs/item"); foreach(XmlNode node in nodes) { ... } something like that should go through the nodes...
-
I've got a set of xml files which all look similar to this one: Standard Das Standard Layout. default.css standard.jpg Text Version Das Leichtgewicht. textonly.css textonly.jpg The number of item elements varies. What is the easiest way to loop through the xml and extract the individual data items? I had a look at the XmlTextReader but the methods provided in this class seem a bit cumbersum. There must be an easier way... Any help is greatly appreciated. /matthias
Here is a simple process the goes through an entire XML file regardless of definition.
XmlTextReader reader = new XmlTextReader(@"..\\..\\test.xml"); try { while (!reader.EOF && reader.NodeType != System.Xml.XmlNodeType.Element) { BuildLine( reader, reader.NodeType.ToString(), reader.Name, reader.Value ); reader.Read(); } if (reader.EOF) { reader.Close(); results.Items.Add("End of file process"); return; } } catch (System.Exception exc) { results.Items.Add(exc.Message); if (exc.InnerException != null) results.Items.Add(exc.InnerException.Message); return; } int currentDepth = reader.Depth; DoAttributes(reader); do { if (reader.NodeType == System.Xml.XmlNodeType.Element) { results.Items.Add(BuildLine( reader, "Element", reader.Name, reader.Value )); if (reader.AttributeCount > 0) { DoAttributes(reader); } reader.Read(); } else { results.Items.Add(BuildLine( reader, reader.NodeType.ToString(), reader.Name, reader.Value )); reader.Read(); } } while (!reader.EOF && !((reader.NodeType == System.Xml.XmlNodeType.EndElement || reader.NodeType == System.Xml.XmlNodeType.Element) && reader.Depth <= currentDepth));
...
private void DoAttributes(XmlTextReader reader) { results.Items.Add(BuildLine( reader, reader.NodeType.ToString(), reader.Name, reader.Value )); if (reader.MoveToFirstAttribute()) { do { results.Items.Add(BuildLine( reader, "Attribute", reader.Name, reader.Value )); }while(reader.MoveToNextAttribute()); } }
There are 10 kinds of people in the world.
Those that read binary...
...and those who don't. -
I've got a set of xml files which all look similar to this one: Standard Das Standard Layout. default.css standard.jpg Text Version Das Leichtgewicht. textonly.css textonly.jpg The number of item elements varies. What is the easiest way to loop through the xml and extract the individual data items? I had a look at the XmlTextReader but the methods provided in this class seem a bit cumbersum. There must be an easier way... Any help is greatly appreciated. /matthias
Both the suggestions submitted are good. If you want a good tutorial on working with XML try Here and look for Lesson 10: Working with XML (NB: The Tutorial is for Visual C# 2005 Express)