|
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>
</Other>
</IsPositions>
</Department>
</CompanyTwo>
<Company Name="CompanyThree">
<Department>
<IsPositions>
<Temp>
<MoreNestedDetails></MoreNestedDetails>
</Temp>
<IsPerm>
<MoreNestedDetails></MoreNestedDetails>
</IsPerm>
<Intern>
<MoreNestedDetails></MoreNestedDetails>
</Intern>
<Other>
<MoreNestedDetails></MoreNestedDetails>
</Other>
</IsPositions>
</Department>
</CompanyThree>
<Company Name="CompanyFour">
<Department>
<IsPositions>
<Temp>
<MoreNestedDetails></MoreNestedDetails>
</Temp>
<IsPerm>
<MoreNestedDetails></MoreNestedDetails>
</IsPerm>
<Intern>
<MoreNestedDetails></MoreNestedDetails>
</Intern>
<Other>
<MoreNestedDetails></MoreNestedDetails>
</Other>
</IsPositions>
</Department>
</CompanyFour>
</MotherCompany>
Now what I need to do is read the information for each Company under MotherCompany.
A - Since its so freaking nested I am confused as to how to go about collecting the information. Any ideas and suggestion?
B - I tried to use XMLReader; I was doing something like:
<br />
XmlTextReader reader = new XmlTextReader(FileNameWithPath); <br />
while (reader.Read())<br />
{<br />
if (reader.Name == "Company")<br />
{<br />
if (reader.NodeType == XmlNodeType.Element)<br />
{<br />
XmlReader currentNode = reader.ReadSubtree();<br />
while (currentNode.Read())<br />
{<br />
if (currentNode.NodeType == XmlNodeType.Element)<br />
{<br />
switch (currentNode.Name)<br />
{<br />
case "Temp<br />
dataCol.MethodList.Add(currentNode.Name);<br />
break;<br />
case "IsPerm<br />
dataCol.MethodList.Add(currentNode.Name);<br />
break;<br />
case "Intern<br />
dataCol.MethodList.Add(currentNode.Name);<br />
break;<br />
case "Other<br />
dataCol.MethodList.Add(currentNode.Name);<br />
break;<br />
default:<br />
break;<br />
}<br />
}<br />
}<br />
}<br />
}<br />
}<br />
First of all my reader.ReadSubtree(); reads all the Company Elements, I thought that it would read any thing within the Company Element so I would be able to get data for seperate Company Elements depending on thier the Name attribute. How can I do that in the given scenario? Is there a better way? Like somehow skip to the elements that I need instead of worrying about the nested loops and crap?
Any help will be great and HIGHLY appreciated. Thanks.
PS: At some point I will need to go and get more details and it looks like I am keep nesting while loops for every detailed value; is there a better way of doing that?
|
|
|
|
|
Use XPathDocument or XmlDocument instead of the XmlTextReader, unless this is a really huge file.
XPathDocument doc = new XPathDocument("MyFile.xml");<br />
XPathNavigator nav = doc.CreateNavigator();<br />
<br />
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.
|
|
|
|
|
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
This will explain why xmlreader was working by xpath logic was not working (yeah it was looping thorugh all the elements ).
Any suggestions?
|
|
|
|
|
XPathDocument doc = new XPathDocument(FileNameWithPath);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator nodes = nav.Select("/MotherCompany/Compant/Department/IsPositions");
XPathNavigator nodesNav = nodes.Current;
XPathNodeIterator ni = nodesNav.SelectDescendants(XPathNodeType.Element, false);
while(nodes.MoveNext())
{
MessageBox.Show(nodes.Current.Name);
richTextBox1.AppendText(Environment.NewLine
+ "Name:" + 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
|
|
|
|
|
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
|
|
|
|
|
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");<br />
foreach(XmlNode position in tempPositions)<br />
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.
|
|
|
|
|
plz, i need some application for educational perpose .
plz, help me .
jignesh patel
|
|
|
|
|
static void Main(string[] args)
{
Console.WriteLine("Hello, world");
}
only two letters away from being an asset
|
|
|
|
|
Best Regards,
Apurva Kaushal
|
|
|
|
|
How can I run a flash file inside an asp .net page?
|
|
|
|
|
|
plz send some .net projects using asp.net,c#,sqlserver2000
|
|
|
|
|
Why? Where to?
---
b { font-weight: normal; }
|
|
|
|
|
r u an student or an Developer. first of all go through the "Chris" first question.
And then post ur questions.
With Regards
Satips
|
|
|
|
|
i have pag to regestration this page contain alot of textbox i want to delet the value in text boxses after end regestration
thank you for help me
ashrafhakiem
|
|
|
|
|
HI,
First say your are cerating an Windows based or Web based form??? and the platform you use
Know is Drop, Unknown is Ocean
|
|
|
|
|
|
make one for loop which counts all objects in ur page and inside on loop ask if control is textbox if so delete text property of control.
|
|
|
|
|
This will do it.
void SetAllInputControlsClassName(Control parent,
string className, string focusClassName)
{
foreach (Control ctl in parent.Controls)
{
if (ctl is TextBox || ctl is ListBox ||
ctl is DropDownList)
{
//Do stuff here
}
}
}
|
|
|
|
|
thank u for helping but i have aproblem
i iwant to clear all textbox but the
ctr.text not avalid
|
|
|
|
|
Sorry, I sent the wrong code snippet.
Try this:
private void Button1_Click(object sender, System.EventArgs e)
{
ClearAllControls( Page.FindControl("Form1"));
}
void ClearAllControls(Control parent)
{
TextBox tb = null;
foreach (Control ctl in parent.Controls)
{
if (ctl is TextBox)
{
tb = (TextBox)ctl;
tb.Text = "";
}
}
}
|
|
|
|
|
Hi Everyone,
I am c# Beginner, can u plz solve my problem like "thread was being aborted" when using try catch(Response.redirect();)
suresh babu.k
|
|
|
|