2012-03-23 26 views
0

私は希望の出力を得るのに少し問題があります。私はXMLを解析し、いくつかのノードinnertextと属性値を変更しようとしています。xmlノードのinnertextと属性値を変更しています

期待通りに機能する属性部分を取得できますが、ノードのinnertext値は取得できません。

サンプルのxml:属性値を置換し、正常に動作するようですし、私が期待するようなXMLを修正

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

をして、 "名前がMyWebSite" が含まれる」:

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

私には、以下の持っています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; 
} 

をしかし、出力は私が期待したいされていないので、これは、明らかに正しくありません。

+0

だけ好奇心が強い、この 'http:// schemas.xmlsoap.org/hand /'名前空間は何ですか? –

+0

申し訳ありませんが、これは単なる例です。しかし、それは手の代わりに "wsdl"でなければなりません。 – Paul

答えて

1

InnerTextは、要素の全体のテキスト内容を返します - サブ要素内に含まれるテキストなどを、そのテスト:

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

はルート1を含め、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) { 
+0

ありがとうMiMo。私はしばらく離れていた。 – Paul

関連する問題