日期:2014-05-17  浏览次数:20385 次

xml节点查询问什么查不到值?
xml页面
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
  <Article name="aa" >
  <a href="Article.aspx?id=44">222</a>
  </Article>
</urlset>
调用页面
  Model.Article model1 = bll.GetModel("Title=\""+model.Title+"\"");
  string xmlPatha = Server.MapPath("../../GoogleSitemaps.xml");
  Common.XmlObject.XmlInsertElement(xmlPatha, "//Article", "Article","<a href=Article.aspx?id="+model1.Id+">"+model.Title+"</a>");
  方法:
 public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
  {
  XmlDocument objXmlDoc = new XmlDocument();
  objXmlDoc.Load(xmlPath);
  XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
  XmlElement objElement = objXmlDoc.CreateElement(Element);
  //objXmlDoc.SelectNodes
  objElement.InnerText = Content;
  objNode.AppendChild(objElement);
  objXmlDoc.Save(xmlPath);
  } 
为什么我拿到的objNode一直是空值? 高手指教...在线等..

------解决方案--------------------
忘了名称空间吧

 XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode,nmr);
------解决方案--------------------
带名称空间的方法

http://msdn.microsoft.com/zh-cn/library/h0hw012b.aspx
C# code

XmlNamespaceManager nsmgr = new XmlNamespaceManager(objXmlDoc.NameTable);
nsmgr.AddNamespace("xx", "http://www.google.com/schemas/sitemap/0.84");

XmlNode objNode = objXmlDoc.SelectSingleNode("//xx:Article",nsmgr);

------解决方案--------------------
xml名称空间是xml技术中的一个重要概念,网上很多讲解了,

就跟你写c#代码中的名称空间作用类似
------解决方案--------------------
你怎么测的啊

C# code
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(Server.MapPath("~/1.XML"));
XmlNamespaceManager nsmgr = new XmlNamespaceManager(objXmlDoc.NameTable);
nsmgr.AddNamespace("xx", "http://www.google.com/schemas/sitemap/0.84");

XmlNode objNode = objXmlDoc.SelectSingleNode("//xx:Article", nsmgr);
Response.Write(objNode.InnerXml);