Tag: default namespace

将XPath与XML命名空间一起使用

我想用XPath处理下面的xml: Tere是根元素的xmlns 。 我的代码是这样的: XElement doc = XElement.Load(xmlFilePath); XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); ns.AddNamespace(“prefix”, “http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration”); XElement settingDiagConnectionString = doc.XPathSelectElement(“//prefix:ServiceConfiguration/Role/ConfigurationSettings/Setting[1]”, ns); // <===== always return null. settingDiagConnectionString.SetAttributeValue("value", "hello, world!"); 但是settingDiagConnectionString始终为null。 为什么? 加1 谢谢har07。 为每个元素添加前缀后,以下代码有效: XmlDocument doc = new XmlDocument(); doc.Load(cscfgPath); XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); ns.AddNamespace(“prefix”, “http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration”); XmlNode settingDiagConnectionString = doc.SelectNodes(“/prefix:ServiceConfiguration/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]”, ns)[0]; settingDiagConnectionString.Attributes[“value”].Value = […]