Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 7, 2010 2:37 PM
Hi
I am attempting to write a piece of code that will extract the Child Elements from a parent XML structure. E.G.
<Parent>
<Child1> <ABC>...</ABC> </Child1>
<Child2> <ABC>...</ABC> </Child2>
<Child3> <ABC>...</ABC> </Child2>
</Parent>
Will become
<Child1> <ABC>...</ABC> </Child1>
<Child2> <ABC>...</ABC> </Child2>
<Child3> <ABC>...</ABC> </Child2>
Note I want this to run against a variety of different files so I cannot use the node names for navigation.
Is this possible using the XMLReader Class. Note I cannot use the DOM as my files are too large. Also requesting the innerXML() of the parent node is still too big. Need to itterate of the child nodes.
Thanks in advance.
Darran
All replies (3)
Thursday, September 9, 2010 2:33 PM ✅Answered
class XmlClass6
{
public static void ReadXml_File()
{
int currentDepth = 0;
FileStream file = new FileStream("XMLFile1.xml", FileMode.OpenOrCreate);
BufferedStream stream = new BufferedStream(file);
using (XmlTextReader reader = new XmlTextReader(stream))
{
while (reader.Read())
{
if (currentDepth < reader.Depth)
{
Debug.Indent();
currentDepth += 1;
}
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Debug.Write("<" + reader.Name);
Debug.WriteLine(">");
if (reader.AttributeCount > 0)
{
WriteReaderAttributes(reader);
}
break;
case XmlNodeType.Text: //Display the text in each element.
Debug.WriteLine(reader.Value.ToString());
break;
case XmlNodeType.EndElement: //Display the end of the element.
Debug.Unindent();
Debug.Write("</" + reader.Name);
Debug.WriteLine(">");
currentDepth -= 1;
break;
case XmlNodeType.Attribute:
Debug.Write(" Attribute ");
Debug.Write("</" + reader.Name + " = " + reader.Value.ToString());
Debug.WriteLine(">");
break;
}
}
reader.Close();
}
stream.Close();
file.Close();
//Console.ReadLine();
return;
}
private static void WriteReaderAttributes(XmlTextReader reader)
{
int attributeCount = reader.AttributeCount;
for (int i = 0; i < attributeCount; i++)
{
reader.MoveToAttribute(i);
Debug.Write(" Attribute ");
Debug.Write("</" + reader.Name + " = " + reader.Value.ToString());
Debug.WriteLine(">");
}
}
}
Your request to use only a specific type makes your question sound like homework. Your type was used extensively with .NET 2.0, which is used by VS2005, which is still finds widespread use in institutions of higher education.
The above class can read every well-formed XML file that I have given it. It does not use your requested type unfortunately. It should not be difficult to modify the method to use your type. Your type is the base class for the type that I used.
Hope this helps.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
http://rudedog2.spaces.live.com/default.aspx
Thursday, September 9, 2010 9:28 AM
Hi DarranShelton,
Welcome to MSDN Forums!
I have not found anything can do this in XmlReader class.
But we can use the following method to find the root node name, and then we can use this name access the root node, after this we can operate the xml as usual to find the children nodes.
public string Look4RootNodeName(string fileName)
{
string rootNode = "";
string s = "";
StreamReader sr = new StreamReader(fileName);
while(sr.EndOfStream!=true)
{
s = sr.ReadLine().Trim();
if(s.IndexOf("<?")!=-1)
{
continue;
}
if (s.IndexOf('<')!=-1)
{
int start = s.IndexOf('<')+1;
int end = s.IndexOf('>', start)-1;
rootNode = s.Substring(start, end);
return rootNode;
}
}
return rootNode;
}
If there’s anything, please feel free to let me know. And I’m glad to help you.
Have a nice day!
Mike
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.
Thursday, September 9, 2010 10:26 AM
List<string> children = new List<string>();
reader.Read(); // Read root
while (!reader.EOF)
{
reader.Read();
if (reader.NodeType == XmlNodeType.Element)
children.Add(reader.ReadOuterXml());
}