【问题标题】:modifying xml node innertext and attributes value修改 xml 节点内部文本和属性值
【发布时间】:2012-03-23 22:16:50
【问题描述】:

我在获得我想要的输出时遇到了一些麻烦。我正在尝试解析 XML 并修改一些节点内部文本和属性值。

我可以让属性部分按预期工作,但不能让节点内部文本值工作。

一个示例 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>

我有以下:

public static void ParseXML(XmlNode root)
{
    foreach (XmlNode node in root.ChildNodes)
    {
        XmlAttributeCollection attributes = node.Attributes;

        if (attributes != null)
            foreach (XmlAttribute attribute in attributes)
            {
                Console.WriteLine(attribute.Name + ": " + attribute.Value);
                if (attribute.Value.Contains("mywebsite"))
                {
                    Console.WriteLine("found an ATTRIBUTE value that contains localhost");
                    string origValue = attribute.Value;
                    string modValue = System.Text.RegularExpressions.Regex.Replace(origValue, "mywebsite(:\\d+){0,1}", "NEW_WEBSITE");
                    attribute.Value = modValue;
                }
                ParseXML(node);
            }
    }
}

这似乎工作正常并按照我的预期修改了xml,将包含“mywebsite”的属性值替换为“NEW_WEBSITE”:

<?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://NEW_WEBSITE/projectappmgr/basic" />
    </hand:port>
    <hand:port name="WSHttpBinding_IprojectAppSvc" binding="tns:WSHttpBinding_IprojectAppSvc">
      <soap12:address location="http://NEW_WEBSITE/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://NEW_WEBSITE/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>

我还想修改以下内文:

 <wsa10:Address>http://mywebsite:5255/projectappmgr/ws</wsa10:Address>

我在 foreach 循环中尝试了以下内容:

 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;
 }

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

【问题讨论】:

  • 只是好奇,http://schemas.xmlsoap.org/hand/ 命名空间是什么?
  • 对不起,这只是一个例子。但它应该是“wsdl”代替手。

标签: c# xml recursion


【解决方案1】:

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")) {

或者您应该测试该节点是一个只有一个子元素(文本)的元素:

 XmlElement element = node as XmlElement;
 if (element != null && 
     element.InnerText.Contains("mywebsite") &&
     element.ChildNodes.Count == 1) {

【讨论】:

  • 感谢 MiMo。我已经离开一段时间了。
猜你喜欢
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 2011-09-28
相关资源
最近更新 更多