xml parser newbie

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gigs

    #1

    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">programm ing xml</name>
    </book>
    <book>
    <name id="2">programm ing 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">programm ing xml</name>
    </katalog>
    </book>
    <book>
    <name id="2">programm ing 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(s tring id, string path, XPathNavigator nav,
    XmlTextWriter writer)
    {
    XPathNodeIterat or iter = nav.Select(path );

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

    writer.WriteSta rtElement(iter. Current.Name);

    if (iter.Current.H asChildren)
    {
    //dont know how to get children path to put it in recursion
    ParseNodeTree(i d, path, nav, writer);
    }
    else
    {
    writer.WriteFul lEndElement();
    }
    }
    }


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


    thanks!
  • Daniel Cigic

    #2
    Re: xml parser newbie

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

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

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




    "gigs" <gigs@hi.t-com.hrwrote in message
    news:g1ph1l$cm0 $1@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">programm ing xml</name>
    </book>
    <book>
    <name id="2">programm ing 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">programm ing xml</name>
    </katalog>
    </book>
    <book>
    <name id="2">programm ing 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(s tring id, string path, XPathNavigator nav,
    XmlTextWriter writer)
    {
    XPathNodeIterat or iter = nav.Select(path );
    >
    while (iter.MoveNext( ))
    {
    if (iter.Current.G etAttribute("id ", "") == id)
    {
    //change this node text with text from first file
    }
    >
    writer.WriteSta rtElement(iter. Current.Name);
    >
    if (iter.Current.H asChildren)
    {
    //dont know how to get children path to put it in recursion
    ParseNodeTree(i d, path, nav, writer);
    }
    else
    {
    writer.WriteFul lEndElement();
    }
    }
    }
    >
    >
    can someone please help me. i lost 2 days for this
    i cant do it anymore
    >
    >
    thanks!

    Comment

    • gigs

      #3
      Re: xml parser newbie

      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.H asChildren)
      {
      //dont know how to get children path to put it in recursion
      XPathNavigator nodesNavigator = nodes.Current;
      XPathNodeIterat or nodes =
      nodesNavigator. SelectChildren( "katalog", "");
      //or check other SelectChildren overloaded method
      >
      while (nodes.MoveNext ())
      // extract infos that you need from nodes.Current
      ParseNodeTree(i d, path, nav, writer);
      }
      >
      >
      >
      >
      "gigs" <gigs@hi.t-com.hrwrote in message
      news:g1ph1l$cm0 $1@ss408.t-com.hr...

      Comment

      • Leon Lambert

        #4
        Re: xml parser newbie

        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">programm ing xml</name>
        </book>
        <book>
        <name id="2">programm ing 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">programm ing xml</name>
        </katalog>
        </book>
        <book>
        <name id="2">programm ing 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(s tring id, string path, XPathNavigator nav,
        XmlTextWriter writer)
        {
        XPathNodeIterat or iter = nav.Select(path );
        >
        while (iter.MoveNext( ))
        {
        if (iter.Current.G etAttribute("id ", "") == id)
        {
        //change this node text with text from first file
        }
        >
        writer.WriteSta rtElement(iter. Current.Name);
        >
        if (iter.Current.H asChildren)
        {
        //dont know how to get children path to put it in recursion
        ParseNodeTree(i d, path, nav, writer);
        }
        else
        {
        writer.WriteFul lEndElement();
        }
        }
        }
        >
        >
        can someone please help me. i lost 2 days for this
        i cant do it anymore
        >
        >
        thanks!

        Comment

        Working...