471,337 Members | 901 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

xml parser newbie

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!
Jun 27 '08 #1
3 1706
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);
}


"gigs" <gi**@hi.t-com.hrwrote in message
news:g1**********@ss408.t-com.hr...
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!
Jun 27 '08 #2
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.

Daniel Cigic wrote:
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);
}


"gigs" <gi**@hi.t-com.hrwrote in message
news:g1**********@ss408.t-com.hr...
Jun 27 '08 #3
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

gigs wrote:
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!
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Rene | last post: by
4 posts views Thread by tuxlover | last post: by
5 posts views Thread by qqcq6s59 | last post: by
2 posts views Thread by Jim Duffie | last post: by
6 posts views Thread by jmike | last post: by
121 posts views Thread by jacob navia | last post: by
15 posts views Thread by an0047 | last post: by
18 posts views Thread by Just Another Victim of the Ambient Morality | last post: by
1 post views Thread by GoodMan | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.