c# 处理带namespace 的 xml

时间:2021-09-12 02:43:31

XmlDocument doc = new XmlDocument();
doc.Load(localxml + "\\" + Path.GetFileName(xinhuaxml));
System.Xml.XmlElement root = doc.DocumentElement;
string namespaces = root.NamespaceURI;
System.Xml.XmlNamespaceManager nsMgr = new System.Xml.XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("apxh", "http://www.w3.org/1999/xhtml");
nsMgr.AddNamespace("apcm", "http://ap.org/schemas/03/2005/apcm");
nsMgr.AddNamespace("ns", namespaces);
string creattime = root.SelectSingleNode(@"//ns:updated", nsMgr).InnerText;
string headlinetxt = root.SelectSingleNode(@"//apcm:SlugLine", nsMgr).InnerText;
string category = root.SelectSingleNode(@"//ns:category", nsMgr).InnerText;
XmlNode content = root.SelectSingleNode(@"//ns:content", nsMgr);