如何更改属性的值?

时间:2014-08-29 06:59:37

标签: c# xml xpath

在最后几天我试图更改此应用程序文件的单个属性的值

Xml-File:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd"
manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
xmlns="urn:schemas-microsoft-com:asm.v2"
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" 
xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"
xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1"
xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
 <assemblyIdentity name="lolz" version="1.1.1.1" publicKeyToken="12345" language="neutral" processorArchitecture="x86" xmlns="urn:schemas-microsoft-com:asm.v1" />
  <description asmv2:publisher="Example" asmv2:product="Productexample2" asmv2:supportUrl="Example" xmlns="urn:schemas-microsoft-com:asm.v1" />
  <deployment install="true" mapFileExtensions="true" minimumRequiredVersion="1.1.1.1" trustURLParameters="true">
   <subscription>
     <update>
        <beforeApplicationStartup />
      </update>
    </subscription>
     <deploymentProvider codebase="http://Test" />
   </deployment>  
</asmv1:assembly>

这里我尝试更改

的值

<description asmv2:product = "Productexample">

进入

<description asmv2:product = "Productexample2">

,以及

的值

<deploymentProvider codebase="http://Test" />

进入

<deploymentProvider codebase="http://Test2" />

目前我尝试过:

private void changeAttribute (string xmlPath)
{
    string newValue = "Productexample2";
    XmlDocument xmlDoc= new XmlDocument();

    xmlDoc.Load(xmlPath);

    XmlNode node = xmlDoc.SelectSingleNode("asmv1:assembly/description/asmv2:product");
    node.Attributes[0].Value = newValue;

    xmlDoc.Save(xmlPath);
}

但它抛出异常&#39;未处理的类型&#39; System.Xml.XPath.XPathException&#39;发生在System.Xml.dll&#39;所以我认为asmv1:assembly/description/asmv2:product是错误的......

任何代码建议?

一如既往,你可以用任何方式纠正我:)

1 个答案:

答案 0 :(得分:0)

您必须使用命名空间管理器才能使前缀有效,并且您需要@字符来表明它是您正在引用的属性。

所以这应该有效:

XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1");
manager.AddNamespace("asmv2", "urn:schemas-microsoft-com:asm.v2");
XmlNode node = xmlDoc.SelectSingleNode("/asmv1:assembly/asmv1:description/@asmv2:product", manager);

此外,XML根目录中的第一行不完整。它应该是:

<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd"
                manifestVersion="1.0"
                xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
                xmlns="urn:schemas-microsoft-com:asm.v2"
                xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
                xmlns:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"
                xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"
                xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1"
                xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2"
>