xml parser newbie

G

gigs

i need to make parser to go trough node tree

i have this structure
<root>
<bookstore pass="1">
<book>
<name id="1">programming xml</name>
</book>
<book>
<name id="2">programming C#</name>
</book>
</bookstore>
</root>
which is not same all the time. sometimes it have more element in book and
sometimes is deeper like this.
this is just example xml
<root>
<bookstore pass="1">
<book>
<katalog num="1">
<name id="1">programming xml</name>
</katalog>
</book>
<book>
<name id="2">programming C#</name>
</book>
</bookstore>
</root>

i have 3 xml files
first xml where i read data and if i have attribute in bookstore i go and check
for name attribute. i took it and than i go to bookstore tag with same
attribute in second xml. and i need to write all elements in bookstore to third
xml (same structure), but i need to change name tag innertext with text from
first xml.

i was thinking

public void ParseNodeTree(string id, string path, XPathNavigator nav,
XmlTextWriter writer)
{
XPathNodeIterator iter = nav.Select(path);

while (iter.MoveNext())
{
if (iter.Current.GetAttribute("id", "") == id)
{
//change this node text with text from first file
}

writer.WriteStartElement(iter.Current.Name);

if (iter.Current.HasChildren)
{
//dont know how to get children path to put it in recursion
ParseNodeTree(id, path, nav, writer);
}
else
{
writer.WriteFullEndElement();
}
}
}


can someone please help me. i lost 2 days for this
i cant do it anymore


thanks!
 
D

Daniel Cigic

Maybe this would help you at least to move to the right direction:

if (iter.Current.HasChildren)
{
//dont know how to get children path to put it in recursion
XPathNavigator nodesNavigator = nodes.Current;
XPathNodeIterator nodes =
nodesNavigator.SelectChildren("katalog", "");
//or check other SelectChildren overloaded method

while (nodes.MoveNext())
// extract infos that you need from nodes.Current
ParseNodeTree(id, path, nav, writer);
}
 
G

gigs

thanks,

i need to check if current node have children, and than get names of that
children because they are different. can i somehow get node child names to put
in nodesNavigator.SelectChildren.
 
D

Daniel Cigic

I think you can just use overload of the SelectChildren mehtod, signature is
something like this:

public virtual XPathNodeIterator SelectChildren(
XPathNodeType type
)

In this case you don't need name(s) of the children elements.
I guess you will need XPathNodeType.Element as a argument for the
SelectChildren in order
to get child elements. This should work I think.
 
L

Leon Lambert

If this exercise is purely academic please ignore this response.

There is a much easier way to deal with XML in .Net. Just make sure you
have a schema (.xsd) file that defines your formating. This should
include all the optional elements you talked about. Then use the .Net
xsd.exe tool to convert the schema into a source file for a strongly
typed dataset. You can then use that file in you project to handle the
xml data. This greatly simplifies the data handling and also give you
some release immunity. For example a bunch of XML methods were
deprecated between .net 1.1 and 2.0 which gave off a lot of warnings. I
had no issues like this when using xsd.exe tool. Of course if this is an
academic problem for learning purposes you can just ignore this advice.

Hope this helps.
Leon Lambert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top