D
D
Dima Avdoshin2020-10-23 16:54:14
C++ / C#
Dima Avdoshin, 2020-10-23 16:54:14

how to read xml in c#?

Good afternoon, how to read xml correctly using .net
Here is the actual part of xml that you need to read

<КонтактнаяИнформация xmlns="http://www.v8.1c.ru/ssl/contactinfo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Представление="141400, Московская обл, Химки г, Коммунальный проезд, владение 30"><Состав xsi:type="Адрес" Страна="Россия"><Состав xsi:type="АдресРФ"><СубъектРФ>Московская обл</СубъектРФ><Город>Химки г</Город><Улица>Коммунальный проезд</Улица><ДопАдрЭл ТипАдрЭл="10100000" Значение="141400"/><ДопАдрЭл><Номер Тип="1020" Значение="30"/></ДопАдрЭл></Состав></Состав></КонтактнаяИнформация>


Here is the code with which I am trying to read it.
The path I set seems to be correct, but the collection is still empty, I don’t understand why, resultStr is a string containing xml
XmlDocument xml = new XmlDocument();
            xml.LoadXml(resultStr);
            XmlNodeList xnList = xml.SelectNodes("/КонтактнаяИнформация/Состав/Состав");
            Console.WriteLine(xnList.Count);
            foreach (XmlNode xn in xnList)
            {
                string city = xn["Город"].InnerText;
                Console.WriteLine("City: {0}", city);
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Avdoshin, 2020-10-23
@D1m0nd

XmlDocument xml = new XmlDocument();
            xml.Load(path);
            foreach (XmlElement element in xml.GetElementsByTagName("Состав"))
            {
                if (element.Attributes["xsi:type"].InnerText == "АдресРФ")
                {
                    foreach (XmlElement e in element)
                        if (e.Name == "Город")
                        {
                            Console.WriteLine("{0} = {1}", e.Name, e.InnerText);
                        }
                        
                }
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question