Quoting via an XML document

My method:

if (File.Exists( @"C:\config.xml")) { System.Xml.XmlDocument xd = new System.Xml.XmlDocument(); xd.Load( @"C:\config.xml"); System.Xml.XmlElement root = xd.DocumentElement; System.Xml.XmlNodeList nl = root.SelectNodes("/config"); foreach (System.Xml.XmlNode xnode in nl) { string name = xnode.Name; string value = xnode.InnerText; string nv = name + "|" + value; Send(nv); } 

My Xml Doc

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <config> <bla>D</bla> <def>300</def> <ttOUT>34000</ttOUT> <num>3800</num> <pw>help</pw> <err>1</err> ....and so on </config> 

Now my method returns the first 2 and nothing more. What am I doing wrong...

+4
source share
2 answers

use System.Xml namespace to avoid a long type qualification, i.e. ....

  using System.Xml; 

Then try something like this.

  XmlNodeList nl = xd.SelectNodes("config"); XmlNode root = nl[0]; foreach (XmlNode xnode in root.ChildNodes) { string name = xnode.Name; string value = xnode.InnerText; string nv = name + "|" + value; Send(nv); } 

I believe something is wrong with your method.

a) I do not think SelectNodes should accept the /config argument, but should accept the config .

b) After selecting the first (and only - XML ​​files in .Net should have one and only one root root) root node, you need to iterate through the ChildNodes root.

+15
source

root is the <config> tag, so I don’t understand how root.SelectNodes ("/ config") should work at all. Use root.Childnodes instead.

+1
source

Source: https://habr.com/ru/post/1336434/


All Articles