P
P
p4p2016-06-05 18:50:33
Parsing
p4p, 2016-06-05 18:50:33

How to parse xml in c#?

Always disliked parsing, hence the gap.
Now I do this, on the server I form a document like

<root>
<top>
<record>Ivan 500</record>
<record>Igor 250</record>
<record>Lena 750</record>
</top>
</root>

Then with a sharp parse:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(callbackLine.ToString());
XmlNodeList xmlnodli =  xmldoc.SelectNodes("Root/top/record");
int num = xmlnodli.Count;
foreach (XmlNode xm in xmlnodli)
{
// тут xm.InnerText; содержит Lena 750 и.т.д
}

but it’s not convenient for me to build an interface because of this, I need Lena to be in a separate variable, and 750 is a separate one. If necessary, an xml document can be created of any kind)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
p4p, 2016-06-05
@p4p

Here is the solution.)

XDocument doc = XDocument.Parse(callbackLine.ToString());
            
            foreach (XElement el in doc.Root.Elements())
            {

                print(el.Attribute("username").Value + " " + el.Attribute("record").Value);
            }

#
#algooptimize #bottize, 2016-06-05
@user004

0. You form the file yourself and complain about its contents yourself ........
1. fill in the gaps
2. there is json, why xml ?
3. in any case, you can do deserialize and not suffer with query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question