Paul
Paul

Reputation: 971

modifying xml node innertext and attributes value

I'm having a bit of trouble obtaining my desired output. I'm trying to parse through XML and modify some node innertext and attribute values.

I'm able to get the attributes portion working as I expect, but not the node innertext value.

A sample 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>

I have the following:

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

Which seems to work fine and modifies the xml as I expect, replacing the attribute values contain "mywebsite" with "NEW_WEBSITE" as:

<?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>

I want to also modify the following innertext:

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

I've tried the following inside the foreach loop:

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

But this is obviously not correct, since the output is not want I expect.

Upvotes: 0

Views: 1761

Answers (1)

MiMo
MiMo

Reputation: 11953

InnerText returns the entire textual content of an element - including the text contained within sub-element, so the test:

if (node.InnerText.Contains("mywebsite")) 

will be true for almost all elements in your XML, including the root one, and when you execute

 node.InnerText = modValue1;   

you wipe out all the sub-elements.

You have to test explicitely for the element you are looking for:

 XmlElement element = node as XmlElement;
 if (element != null && 
     element.LocalName == "address" && 
     element.InnerText.Contains("mywebsite")) {

or you should test that the node is an element with only one child (the text):

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

Upvotes: 1

Related Questions