xpath problem

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

    xpath problem

    How can i get children from specific parent if i have 2 or more same
    parent with same childre? only difference is text inside children

    <root>
    <parent sifra="G1">
    <child id=1>first text>/child>
    <child id="2">second text</child>
    </parent>
    <parent sifra="G1">
    <child id=1>third text>/child>
    </parent>
    </root>

    i need to get "first text" first and than in another temp xmldocument
    change some tag value with this one, if parent have more children,
    than process them and write temp xml to third xml file and check for
    second parent with sifra and get third text and same thing

    thanks!
  • Martin Honnen

    #2
    Re: xpath problem

    Gigs_ wrote:
    How can i get children from specific parent if i have 2 or more same
    parent with same childre? only difference is text inside children
    >
    <root>
    <parent sifra="G1">
    <child id=1>first text>/child>
    <child id="2">second text</child>
    </parent>
    <parent sifra="G1">
    <child id=1>third text>/child>
    </parent>
    </root>
    >
    i need to get "first text" first and than in another temp xmldocument
    change some tag value with this one, if parent have more children,
    than process them and write temp xml to third xml file and check for
    second parent with sifra and get third text and same thing
    Assuming the XML looks like this

    <root>
    <parent sifra="G1">
    <child id="1">first text</child>
    <child id="2">second text</child>
    </parent>
    <parent sifra="G1">
    <child id="1">third text</child>
    </parent>
    </root>

    then this C# code

    XPathDocument doc = new XPathDocument(@ "..\..\XMLFile2 .xml");
    foreach (XPathNavigator parent in
    doc.CreateNavig ator().Select(" root/parent[@sifra = 'G1']"))
    {
    XPathNavigator child1 =
    parent.SelectSi ngleNode("child[@id = '1']");
    if (child1 != null)
    {
    Console.WriteLi ne(child1.Value );
    }
    }

    outputs

    first text
    third text

    which I think is part of what you want to achieve, namely iterating over
    the parent elements where the sifra attribute has the value 'G1' and
    outputting the contents of the child element with id being '1'.


    --

    Martin Honnen --- MVP XML

    Comment

    Working...