This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13633|Reply: 0

[Source] C# reads XML format files with XMLNS properties

[Copy link]
Posted on 7/11/2015 5:39:32 PM | | |

1 file like xmlns="
Example XML file:
<?xml version="1.0" encoding="UTF-8"?>
<WebSrvMessage xmlns="http://www.lenoval.com/">
  <version>1.0</version>
  <DataContent>
    <DateTag>2010-5-17</DateTag>
  </DataContent>
</WebSrvMessage>

XmlDocument doc = new XmlDocument();
doc. Load(path);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc. NameTable);
Add a namespace, and the prefix can be named at will
nsmgr. AddNamespace("lvl", "http://www.lenoval.com/");

//在路径前面使用前面定义的前缀
XmlNode datatag = doc. DocumentElement.SelectSingleNode("lvl:QryDataContent/lvl:DateTag", nsmgr);

2 Files like xmlns:xxxx="
For example: xmlns:lenoval="http://www.lenoval.com/">
You can add a namespace to the code above:
Add a namespace, and the prefix can be named at will
nsmgr. AddNamespace("lenoval", "http://www.lenoval.com/");

//在路径前面使用前面定义的前缀
XmlNode datatag = doc. DocumentElement.SelectSingleNode("lenoval:QryDataContent/lenoval:DateTag", nsmgr);
  1. /// <summary>
  2.         /// 读取网站地图xml
  3.         /// </summary>
  4.         /// <param name="code"></param>
  5.         /// <returns></returns>
  6.         public static List<string> GetUrl(string code)
  7.         {
  8.             List<string> list = new List<string>();
  9.             XmlDocument d = new XmlDocument();
  10.             d.Load(code);
  11.             XmlNamespaceManager m = new XmlNamespaceManager(d.NameTable);
  12.             m.AddNamespace("x", "http://www.sitemaps.org/schemas/sitemap/0.9");
  13.             XmlNodeList url = d.SelectNodes("//x:url", m);
  14.             foreach (XmlNode nd in url)
  15.             {
  16.                 XmlNode loc = nd.SelectSingleNode("x:loc", m);
  17.                 list.Add(loc.InnerText);
  18.             }
  19.             return list;
  20.         }
Copy code






Previous:In C#, in the new version of Newtonsoft, Javascrip removed the workaround that tConvert could not find
Next:C# reads the XML file node contents
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com