我正在尝试更改实例中的count属性,xml低于
<ServiceConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" serviceName="" osFamily="1" osVersion="*"
xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
<Role name="WebRole1">
<ConfigurationSettings>
<Setting name="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
</ConfigurationSettings>
<Instances count="1" />
<Certificates />
</Role>
</ServiceConfiguration>
我在另一个问题中尝试了以下内容,但是我收到错误“对象引用未设置为对象的实例。”
changeConfigXDoc.Root.Element("ServiceConfiguration").Element("Role").Element("Instances").Attribute("count").Value=ChangeInstanceText.Text;
答案 0 :(得分:1)
Root是<ServiceConfiguration />
节点,请尝试这样做:
changeConfigXDoc.Root.Element("Role")
.Element("Instances")
.Attribute("count").Value = ChangeInstanceText.Text;
添加these extension methods,然后尝试此操作,
changeConfigXDoc.Root.Set("Role/Instances/count", ChangeInstanceText.Text, true);
答案 1 :(得分:1)
您应该考虑名称空间
XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
changeConfigXDoc
.Element(ns + "ServiceConfiguration")
.Element(ns + "Role")
.Element(ns + "Instances")
.Attribute("count").Value = ChangeInstanceText.Text;
或者
XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
changeConfigXDoc
.Descendants(ns+"Instances")
.First()
.Attribute("count").Value = "666";