471,337 Members | 887 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.

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!
Jun 27 '08 #1
1 1207
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.CreateNavigator().Select("root/parent[@sifra = 'G1']"))
{
XPathNavigator child1 =
parent.SelectSingleNode("child[@id = '1']");
if (child1 != null)
{
Console.WriteLine(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
http://JavaScript.FAQTs.com/
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Son KwonNam | last post: by
5 posts views Thread by jorgedelgadolopez | 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.