C# 修改xml节点innertext和属性值

C# 修改xml节点innertext和属性值,c#,xml,recursion,C#,Xml,Recursion,我在获得期望的输出时遇到了一些困难。我试图通过XML解析并修改一些节点innertext和属性值 我能够让attributes部分按预期工作,但不能得到node innertext值 示例xml: <?xml version="1.0" encoding="utf-8"?> <hand:definitions xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsa10="htt

我在获得期望的输出时遇到了一些困难。我试图通过XML解析并修改一些节点innertext和属性值

我能够让attributes部分按预期工作,但不能得到node innertext值

示例xml:

<?xml version="1.0" encoding="utf-8"?>
<hand:definitions xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:tns="http://joker.org/" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/hand/contract" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:soap="http://schemas.xmlsoap.org/hand/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsaw="http://www.w3.org/2006/05/addressing/hand" xmlns:soap12="http://schemas.xmlsoap.org/hand/soap12/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="projectAppSvc" targetNamespace="http://joker.org/" xmlns:hand="http://schemas.xmlsoap.org/hand/">
  <hand:service name="projectAppSvc">
    <hand:port name="BasicHttpBinding_IprojectAppSvc" binding="tns:BasicHttpBinding_IprojectAppSvc">
      <soap:address location="http://mywebsite:5255/projectappmgr/basic" />
    </hand:port>
    <hand:port name="WSHttpBinding_IprojectAppSvc" binding="tns:WSHttpBinding_IprojectAppSvc">
      <soap12:address location="http://mywebsite:5255/projectappmgr/ws" />
      <wsa10:Reference>
        <wsa10:Address>http://mywebsite:5255/projectappmgr/ws</wsa10:Address>
          <id xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingid">
            <Upn>number</Upn>
          </id>
       </wsa10:Reference>
     </hand:port>
   <hand:port name="NetTcpBinding_IprojectAppSvc" binding="tns:NetTcpBinding_IprojectAppSvc">
       <soap12:address location="net.tcp://mywebsite:5256/projectappmgr" />
       <wsa10:Reference>
          <wsa10:Address>net.tcp://mywebsite:5256/projectappmgr</wsa10:Address>
             <id xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingid">
                <Upn>number</Upn>
             </id>
          </wsa10:Reference>
       </hand:port>
 </hand:service>
</hand:definitions>
它似乎工作正常,并按照我的预期修改了xml,将包含“mywebsite”的属性值替换为“NEW_WEBSITE”,如下所示:


但这显然是不正确的,因为输出不是我所期望的。

InnerText
返回元素的整个文本内容,包括子元素中包含的文本,因此测试:

if (node.InnerText.Contains("mywebsite")) 
对于XML中的几乎所有元素(包括根元素)都是如此,并且在执行时也是如此

 node.InnerText = modValue1;   
删除所有子元素

您必须针对要查找的元素进行明确的测试:

 XmlElement element = node as XmlElement;
 if (element != null && 
     element.LocalName == "address" && 
     element.InnerText.Contains("mywebsite")) {
或者,您应该测试节点是否是只有一个子元素(文本)的元素:


只是好奇,这是什么
http://schemas.xmlsoap.org/hand/
namespace?抱歉,这只是一个示例。但它应该是“wsdl”而不是手工的。谢谢。我已经离开一段时间了。
 if (node.InnerText.Contains("mywebsite"))
 {
     Console.WriteLine("found an NODE value that contains "mywebsite");
     string origValue1 = node.InnerText;
     string modValue1 = System.Text.RegularExpressions.Regex.Replace(origValue1,
                                                                 "mywebsite(:\\d+){0,1}",
                                                                             "NEW_WEBSITE");
     node.InnerText = modValue1;
 }
if (node.InnerText.Contains("mywebsite")) 
 node.InnerText = modValue1;   
 XmlElement element = node as XmlElement;
 if (element != null && 
     element.LocalName == "address" && 
     element.InnerText.Contains("mywebsite")) {
 XmlElement element = node as XmlElement;
 if (element != null && 
     element.InnerText.Contains("mywebsite") &&
     element.ChildNodes.Count == 1) {