Help with reading XML with C#
-
Hello everyone: I am struggling with XML (which I am really not good at), the file that I am working with is something like this:
<MotherCompany Name="MC"> <Company Name="CompanyOne"> <Department> <IsPositions> <Temp> <MoreNestedDetails></MoreNestedDetails> </Temp> <IsPerm> <MoreNestedDetails></MoreNestedDetails> </IsPerm> <Intern> <MoreNestedDetails></MoreNestedDetails> </Intern> <Other> <MoreNestedDetails></MoreNestedDetails> </Other> </IsPositions> </Department> </CompanyOne> <Company Name="CompanyTwo"> <Department> <IsPositions> <Temp> <MoreNestedDetails></MoreNestedDetails> </Temp> <IsPerm> <MoreNestedDetails></MoreNestedDetails> </IsPerm> <Intern> <MoreNestedDetails></MoreNestedDetails> </Intern> <Other> <MoreNestedDetails></MoreNestedDetails> &n
-
Hello everyone: I am struggling with XML (which I am really not good at), the file that I am working with is something like this:
<MotherCompany Name="MC"> <Company Name="CompanyOne"> <Department> <IsPositions> <Temp> <MoreNestedDetails></MoreNestedDetails> </Temp> <IsPerm> <MoreNestedDetails></MoreNestedDetails> </IsPerm> <Intern> <MoreNestedDetails></MoreNestedDetails> </Intern> <Other> <MoreNestedDetails></MoreNestedDetails> </Other> </IsPositions> </Department> </CompanyOne> <Company Name="CompanyTwo"> <Department> <IsPositions> <Temp> <MoreNestedDetails></MoreNestedDetails> </Temp> <IsPerm> <MoreNestedDetails></MoreNestedDetails> </IsPerm> <Intern> <MoreNestedDetails></MoreNestedDetails> </Intern> <Other> <MoreNestedDetails></MoreNestedDetails> &n
Use XPathDocument or XmlDocument instead of the XmlTextReader, unless this is a really huge file.
XPathDocument doc = new XPathDocument("MyFile.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator nodes = nav.Select("//Company");
Now you should have a collection of Company nodes to iterate through
only two letters away from being an asset
-
Use XPathDocument or XmlDocument instead of the XmlTextReader, unless this is a really huge file.
XPathDocument doc = new XPathDocument("MyFile.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator nodes = nav.Select("//Company");
Now you should have a collection of Company nodes to iterate through
only two letters away from being an asset
Mark: Thanks a lot for your suggestion. This document is typically more then 500 lines but the Company Nodes are not the one that increase the file size, its other junk thats embeded in it. Do you think it would be ok to use XPathDocument for such size files? Thanks.
-
Mark: Thanks a lot for your suggestion. This document is typically more then 500 lines but the Company Nodes are not the one that increase the file size, its other junk thats embeded in it. Do you think it would be ok to use XPathDocument for such size files? Thanks.
It should be fine as long as the file size isn't more than say 1mb
only two letters away from being an asset
-
It should be fine as long as the file size isn't more than say 1mb
only two letters away from being an asset
Well just came to find out that the file I've been working with is not an xml file instead it is an xsd file:confused: This will explain why xmlreader was working by xpath logic was not working (yeah it was looping thorugh all the elements:-O). Any suggestions?
-
Hello everyone: I am struggling with XML (which I am really not good at), the file that I am working with is something like this:
<MotherCompany Name="MC"> <Company Name="CompanyOne"> <Department> <IsPositions> <Temp> <MoreNestedDetails></MoreNestedDetails> </Temp> <IsPerm> <MoreNestedDetails></MoreNestedDetails> </IsPerm> <Intern> <MoreNestedDetails></MoreNestedDetails> </Intern> <Other> <MoreNestedDetails></MoreNestedDetails> </Other> </IsPositions> </Department> </CompanyOne> <Company Name="CompanyTwo"> <Department> <IsPositions> <Temp> <MoreNestedDetails></MoreNestedDetails> </Temp> <IsPerm> <MoreNestedDetails></MoreNestedDetails> </IsPerm> <Intern> <MoreNestedDetails></MoreNestedDetails> </Intern> <Other> <MoreNestedDetails></MoreNestedDetails> &n
The XmlReader probably isn't the way to go here. Load your xml into an XmlDocument and use the SelectNodes method to select the specific nodes you want, for example you could do something like this:
XmlNodeList tempPositions = myXml.SelectNodes("/Company/Department/Temp"); foreach(XmlNode position in tempPositions) tempPositions.Add(position.Name);
The best thing you could do is get familiar with XPath queries and use them to extract the specific information you need from your XmlDocument.Jim Conigliaro jconigliaro@ieee.org
http://www.jimconigliaro.com -
Use XPathDocument or XmlDocument instead of the XmlTextReader, unless this is a really huge file.
XPathDocument doc = new XPathDocument("MyFile.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator nodes = nav.Select("//Company");
Now you should have a collection of Company nodes to iterate through
only two letters away from being an asset
XPathDocument doc = new XPathDocument(FileNameWithPath);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator nodes = nav.Select("/MotherCompany/Compant/Department/IsPositions");
//string x = nodes.Current.GetAttribute("Name", nodes.Current.NamespaceURI);
XPathNavigator nodesNav = nodes.Current;
XPathNodeIterator ni = nodesNav.SelectDescendants(XPathNodeType.Element, false);
while(nodes.MoveNext())
{
MessageBox.Show(nodes.Current.Name);
//nodes.Current.GetAttribute("Name",nodes.Current.NamespaceURI);
// MessageBox.Show(ni.Current.Name + "\n" + ni.Current.Value);
richTextBox1.AppendText(Environment.NewLine
+ "Name:" + /*x +*/ Environment.NewLine
+ nodes.Current.Name + Environment.NewLine +
nodes.Current.Value
);
}When I look in the debug mode to see what is going; this is what I get for my ni Position=0, Current={Root}, Count=0 And aparently nothing happends... Can someone please help me read this document? I sure would appreciate it. Thanks. Regards,Robert
-
XPathDocument doc = new XPathDocument(FileNameWithPath);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator nodes = nav.Select("/MotherCompany/Compant/Department/IsPositions");
//string x = nodes.Current.GetAttribute("Name", nodes.Current.NamespaceURI);
XPathNavigator nodesNav = nodes.Current;
XPathNodeIterator ni = nodesNav.SelectDescendants(XPathNodeType.Element, false);
while(nodes.MoveNext())
{
MessageBox.Show(nodes.Current.Name);
//nodes.Current.GetAttribute("Name",nodes.Current.NamespaceURI);
// MessageBox.Show(ni.Current.Name + "\n" + ni.Current.Value);
richTextBox1.AppendText(Environment.NewLine
+ "Name:" + /*x +*/ Environment.NewLine
+ nodes.Current.Name + Environment.NewLine +
nodes.Current.Value
);
}When I look in the debug mode to see what is going; this is what I get for my ni Position=0, Current={Root}, Count=0 And aparently nothing happends... Can someone please help me read this document? I sure would appreciate it. Thanks. Regards,Robert
XPathNodeIterator nodes = nav.Select("/MotherCompany/Compant/Department/IsPositions"); nodes.MoveNext(); XPathNavigator nodesNav = nodes.Current; The iterator is not positioned at the start so you must move to an element before accessing anything.
only two letters away from being an asset
-
XPathNodeIterator nodes = nav.Select("/MotherCompany/Compant/Department/IsPositions"); nodes.MoveNext(); XPathNavigator nodesNav = nodes.Current; The iterator is not positioned at the start so you must move to an element before accessing anything.
only two letters away from being an asset
Thanks for your help and patience with me Mark. However I still dont get any thing even after putting nodes.MoveNext(). Do you think that
<xs:schema>, <xs:annotation>, <xs:appinfo>
(which are at the top of the document and<MotherCompany>
is nested within<xs:appinfo>
) has anything to do with it? Appreciate your help. Regards, Robert