Compare childnode with C# and XML variable (Xpath)

Asked

Viewed 77 times

1

I have an xml file with several structures. I have a structure called Product who has a field ProductCode and I have a structure called SalesInvoice, inside of it I have a field Invoice, within the Invoice there’s another ProductCode.

I have to compare the ProductCode of the two structures. If they are equal I have to do one thing, if they are different I have to do something else.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

It’s a little confusing but I explained the best I could, I hope you understand. I’m using C#!

  • 1

    What have you tried ?

  • @LP.Gonçalves already tried to use Selectnodes, Selectsinglenode so that didn’t work needed a help, I’ve been in this situation for 1 week

  • You could update your question with this code snippet from which you tried to use Selectnodes, Selectsinglenode ?

  • You need to learn the algorithms used to traverse a tree. Without that knowledge, even with the help of the people here it will be difficult to solve your problem.

  • 1

    @Renan I know how to use algorithms to go through a tree but I don’t know for a more complex situation like this

  • Until you update your question, it might help(it is in English) http://stackoverflow.com/questions/890313/how-to-use-a-linq-query-to-get-xelement-values-when-xelements-have-same-name

  • In fact the situation you have is the simplest application of the pre-order, in-order, post-order algorithms etc. You need to go through a tree and find the productCode nodes and compare their values.

  • @Renan Exactly ! that’s right. The Productcode of the first structure (Product) I can get, I already have an algorithm to get it but I can’t get the other one (from the Salesinvoices structure). Compare them I think also with you, I just needed help to get it and maybe keep it in a variable to be easier

  • @Will Renan help me in this part of the algorithm ? I’ve been in it for 1 week and I can’t. It was really important

  • 1

    It would be better to post your code including XML because image becomes complicated to give a solution...

  • I already got what I wanted. Only now I have another problem. There are Productcodes with repeated values but I just want to get the first one, anyone know how? Here is my code: Xmlnodelist xnList = doc.Getelementsbytagname("Line"); foreach (Xmlnode Xn in xnList) { string salescode = Xn["Productcode"]. Innertext; if (salescode == code) { Console.Writeline(salescode); } Else; }

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.