C# 将XPath与XML命名空间一起使用
我想用XPath处理下面的xml:C# 将XPath与XML命名空间一起使用,c#,xml,xpath,xml-namespaces,default-namespace,C#,Xml,Xpath,Xml Namespaces,Default Namespace,我想用XPath处理下面的xml: <?xml version="1.0" encoding="utf-8"?> <ServiceConfiguration serviceName="Cloud" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2014-01.2.3">
<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="Cloud" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2014-01.2.3">
<Role name="Worker">
<Instances count="2" />
<ConfigurationSettings>
<Setting name="setting1" value="value1" />
<Setting name="setting2" value="value2" />
</ConfigurationSettings>
<Certificates>
</Certificates>
</Role>
</ServiceConfiguration>
但是设置DiagConnectionString
始终为空
为什么?
加1
谢谢你
为每个元素添加前缀后,以下代码起作用:
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 = "hello,world!";
但是下面的代码仍然不起作用。设置DiagConnectionString
仍然为空。为什么?
XElement doc = XElement.Load(cscfgPath);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("prefix", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
XElement settingDiagConnectionString = doc.XPathSelectElement("/prefix:ServiceConfiguration/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]", ns);
settingDiagConnectionString.SetAttributeValue("value", "hello, world!");
默认名称空间具有不同的性质。在同一默认名称空间中声明默认名称空间的元素及其所有子代(未考虑不同的名称空间声明)。因此,您还需要为所有子代使用前缀。这个XPath对我来说很好:
XElement doc = XElement.Parse(xml);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("prefix", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
XElement settingDiagConnectionString = doc.XPathSelectElement("//prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]", ns);
settingDiagConnectionString.SetAttributeValue("value", "hello, world!");
更新:
回应你的更新。这是因为您使用的是XElement
。在本例中,doc
本身已经表示了
元素,因此不需要在XPath中包含“ServiceConfiguration”:
/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]
…或使用
XDocument
而不是XElement
,如果您希望使用与XmlDocument
相同的XPath使其工作,则默认命名空间具有不同的性质。在同一默认名称空间中声明默认名称空间的元素及其所有子代(未考虑不同的名称空间声明)。因此,您还需要为所有子代使用前缀。这个XPath对我来说很好:
XElement doc = XElement.Parse(xml);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("prefix", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
XElement settingDiagConnectionString = doc.XPathSelectElement("//prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]", ns);
settingDiagConnectionString.SetAttributeValue("value", "hello, world!");
更新:
回应你的更新。这是因为您使用的是XElement
。在本例中,doc
本身已经表示了
元素,因此不需要在XPath中包含“ServiceConfiguration”:
/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]
…或使用
XDocument
而不是XElement
,如果您希望使用与XmlDocument
相同的XPath使其工作,则默认命名空间具有不同的性质。在同一默认名称空间中声明默认名称空间的元素及其所有子代(未考虑不同的名称空间声明)。因此,您还需要为所有子代使用前缀。这个XPath对我来说很好:
XElement doc = XElement.Parse(xml);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("prefix", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
XElement settingDiagConnectionString = doc.XPathSelectElement("//prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]", ns);
settingDiagConnectionString.SetAttributeValue("value", "hello, world!");
更新:
回应你的更新。这是因为您使用的是XElement
。在本例中,doc
本身已经表示了
元素,因此不需要在XPath中包含“ServiceConfiguration”:
/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]
…或使用
XDocument
而不是XElement
,如果您希望使用与XmlDocument
相同的XPath使其工作,则默认命名空间具有不同的性质。在同一默认名称空间中声明默认名称空间的元素及其所有子代(未考虑不同的名称空间声明)。因此,您还需要为所有子代使用前缀。这个XPath对我来说很好:
XElement doc = XElement.Parse(xml);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("prefix", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
XElement settingDiagConnectionString = doc.XPathSelectElement("//prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]", ns);
settingDiagConnectionString.SetAttributeValue("value", "hello, world!");
更新:
回应你的更新。这是因为您使用的是XElement
。在本例中,doc
本身已经表示了
元素,因此不需要在XPath中包含“ServiceConfiguration”:
/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]
…或者使用
XDocument
而不是XElement
如果您想让它使用与XmlDocument
相同的XPath工作,谢谢,这非常有用。我更新了我的问题。请参阅更新部分中我的简短回答。非常感谢您的回答。谢谢,这非常有帮助。我更新了我的问题。请参阅更新部分中我的简短回答。非常感谢您的回答。谢谢,这非常有帮助。我更新了我的问题。请参阅更新部分中我的简短回答。非常感谢您的回答。谢谢,这非常有帮助。我更新了我的问题。见更新部分我的简短回答。非常感谢你的回答。