2014-04-16 37 views
0

我試着以取代123456789的節點值到100200525. XML是:試圖取代XML節點值

enter code here 

<?xml version="1.0" encoding="UTF-8"?> 
-<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"> 
    +<SOAP:Header> 
    ----<SOAP:Body> 
     -<SignOnRq xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1.2.0/xml/"> 
     -<testApp> 
      <Org>Online</Org> 
      <TestInitiated>Online</TestInitiated> 
     </testApp> 
     </SignOnRq>-<variablesAutounityQuoteInqRq xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1.2.0/xml/"> 
     <RqUID>xxxx-xxxxx-xxxxx-xxx-xxxxxx</RqUID><TransactionRequestDt>2014-04-11T11:31:32</TransactionRequestDt><CurCd>HK</CurCd><InsuredOrPrincipal/>-<variablesunity> 
      <TestCd>AUTOP</TestCd>-<TestTerm> 
      <EffectiveDt>2014-05-04</EffectiveDt> 
      </TestTerm><PrintedDocumentsRequestedInd>0</PrintedDocumentsRequestedInd>-<OtherOrPriorunity> 
      <unityCd>Prior</unityCd><LOBCd>AUTOP</LOBCd><TesterName>CEN</TesterName>-<TestTerm> 
       <ExpirationDt>2014-05-05</ExpirationDt> 
      </TestTerm>-<LengthTimeWithPreviousTester> 
       <NumUnits>5</NumUnits> 
       <UnitMeasurementCd>Year</UnitMeasurementCd> 
      </LengthTimeWithPreviousTester>-<test> 
       <testCd>BDY</testCd><testDesc>25/50</testDesc>-<Limit> 
       <FormatInteger>25000</FormatInteger> 
       <LimitAppliesToCd>variablevariableson</LimitAppliesToCd> 
       </Limit>-<Limit> 
       <FormatInteger>50000</FormatInteger> 
       <LimitAppliesToCd>variableAcc</LimitAppliesToCd> 
       </Limit> 
      </test> 
      </OtherOrPriorunity>-<variablesApplicationInfo> 
      -<InsuredOrPrincipal> 
       -<GeneralPartyInfo> 
       -<NameInfo> 
        +<variablesonName> 
        -<EmpIdentity> 
         <EmpTypeCd>BADGE</EmpTypeCd> 
         <EmpId>123456789</EmpId> 
        </EmpIdentity> 
       </NameInfo>-<Addr> 
        <AddrTypeCd>Living</AddrTypeCd> 
        <Addr1>500 bronze st</Addr1> 
        <City>TAMPA</City> 
        <StateProvCd>FL</StateProvCd> 
        <PostalCode>33603</PostalCode> 
       </Addr>-<Communications> 
        -<EmailInfo> 
        <CommunicationUseCd>Business</CommunicationUseCd> 
        <EmailAddr>[email protected]</EmailAddr> 
        </EmailInfo> 
       </Communications> 
       </GeneralPartyInfo> 
      </testerOrPrincipal><ResidenceTypeCd>2</ResidenceTypeCd> 
      </variablesApplicationInfo>-<testermover moverRef="mover0" testerRef="Drv0"> 
      <UsePct>100</UsePct> 
      </testermover> 
     </variablesunity>-<variablesAutoLineBusiness> 
      <LOBCd>AUTOP</LOBCd>-<variablestester id="Drv0"> 
      -<GeneralPartyInfo> 
       -<NameInfo> 
       -<variablesonName> 
        <Surname>dust</Surname> 
        <GivenName>bronze</GivenName> 
       </variablesonName> 
       </NameInfo> 
      </GeneralPartyInfo>-<testerInfo> 
       -<variablesonInfo> 
       <GenderCd>M</GenderCd> 
       <BirthDt>1970-01-01</BirthDt> 
       <MaritalStatusCd>S</MaritalStatusCd> 
       <OccupationClassCd>5</OccupationClassCd> 
       <EducationTypeCd>13</EducationTypeCd> 
       </variablesonInfo>-<testersLicense> 
       <LicensedDt>1986-01-01</LicensedDt> 
       <testersLicense>UNKNOWN</testersLicense> 
       <SR22>N</SR22> 
       </testersLicense> 
      </testerInfo>-<variablestesterInfo> 
       <testerRelationshipToApplicantCd>SLF</testerRelationshipToApplicantCd> 
       <testerTypeCd>I</testerTypeCd> 
      </variablestesterInfo> 
      </variablestester>-<variablesmover id="mover0"> 
      <LeasedmoverInd>0</LeasedmoverInd><moverIdentification>BBTYUJUI</moverIdentification><AnnualMileage>12000</AnnualMileage>-<DistanceOneWay> 
       <NumUnits>10</NumUnits> 
       <UnitMeasurementCd>Mile</UnitMeasurementCd> 
      </DistanceOneWay><moverUseCd>C</moverUseCd>-<test> 
       <testCd>BDY</testCd><testDesc>Limits</testDesc>-<Limit> 
       <FormatInteger>25000</FormatInteger> 
       <LimitAppliesToCd>variablevariableson</LimitAppliesToCd> 
       </Limit>-<Limit> 
       <FormatInteger>50000</FormatInteger> 
       <LimitAppliesToCd>variableAcc</LimitAppliesToCd> 
       </Limit> 
      </test>-<test> 
       <testCd>PRP</testCd><testDesc>Limits</testDesc>-<Limit> 
       <FormatInteger>25000</FormatInteger> 
       </Limit> 
      </test>-<test> 
       <testCd>CMP</testCd> 
       <testDesc>No test</testDesc> 
      </test>-<test> 
       <testCd>COL</testCd> 
       <testDesc>No test</testDesc> 
      </test>-<test> 
       <testCd>UUB</testCd><testDesc>gif</testDesc>-<Limit> 
       <FormatInteger>10000</FormatInteger> 
       <LimitAppliesToCd>variablevariableson</LimitAppliesToCd> 
       </Limit>-<Limit> 
       <FormatInteger>20000</FormatInteger> 
       <LimitAppliesToCd>variableAcc</LimitAppliesToCd> 
       </Limit> 
      </test>-<test> 
       <testCd>MED</testCd> 
       <testDesc>I decline</testDesc> 
      </test>-<test> 
       <testCd>PIP</testCd> 
       <testDesc>Basic</testDesc> 
      </test>-<test> 
       <testCd>PDO</testCd> 
       <testDesc>Named tester & Relatives</testDesc> 
      </test>-<test> 
       <testCd>PIL</testCd>-<Limit> 
       <FormatInteger>10000</FormatInteger> 
       </Limit> 
      </test>-<test> 
       <testCd>PID</testCd>-<Deductible> 
       <FormatInteger>1000</FormatInteger> 
       </Deductible> 
      </test>-<test> 
       <testCd>PWX</testCd> 
       <testDesc>Incl. work loss test</testDesc> 
      </test>-<test> 
       <testCd>RCR</testCd> 
       <testDesc>I decline</testDesc> 
      </test>-<test> 
       <testCd>ERS</testCd> 
       <testDesc>I decline</testDesc> 
      </test>-<test> 
       <testCd>Discounts</testCd><testDesc>discounts</testDesc>-<cdOrSurcharge> 
       <cdSurchargeCd>ATFAP</cdSurchargeCd> 
       </cdOrSurcharge> 
      </test> 
      </variablesmover> 
     </variablesAutoLineBusiness> 
     </variablesAutounityQuoteInqRq> 
    </SOAP:Body> 
</SOAP:Envelope> 

解決方案我想這力工作:

//開始

XmlNodeList BadgeNodes = xmlDocument.SelectNodes("//EmpId"); 
if ((taxIdNodeList != null) && (taxIdNodeList.Count > 0)) 
{ 
    foreach (XmlNode Badge in BadgeNodes) 
    { 
     Badge.InnerText = "100200525"; 
    } 
} 

//結束

我試過Linq。當我提到rootnode時,我嘗試通過上面的層次結構到達「EmpId」節點。

XElement xElement = XElement.Load(new XmlNodeReader(xmlDoc)); 
      var badgeList = 
       xElement.Elements("rootnode1").Elements("rootnode2").Elements("rootnode3").Elements("rootnode4"). 
       Elements("rootnode5").Elements("rootnode6").Elements("rootnode7").Elements("EmpId").ToList(); 
      foreach (XElement badge in badgeList) 
      { 
       badge.ReplaceNodes("100200525"); 
      } 

需要幫助

+0

您最好發佈實際的XML,而不是這個漂亮的版本,這樣就不可能測試您擁有的東西。無論如何,這很可能是您的查詢中缺少名稱空間標識符。 – voidengine

回答

0

您的XML具有默認命名空間聲明的位置:

<variablesAutounityQuoteInqRq 
    xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1.2.0/xml/"> 

這意味着,上述元素和它的所有後代無前綴(並沒有宣佈一個不同的默認命名空間)考慮在相同的命名空間中。因此,在上面的命名空間中考慮了<EmpId>元素。

訪問名稱空間中元素的可能方法是聲明指向名稱空間URI的前綴。然後在查詢中使用該前綴,例如:

var nsManager = new XmlNamespaceManager(xmlDocument.NameTable); 
nsManager.AddNamespace("d", "http://www.ACORD.org/standards/PC_Surety/ACORD1.2.0/xml/"); 
XmlNode specificAction = xmlDoc.DocumentElement 
           .SelectSingleNode("//d:EmpId", nsManager); 
+0

感謝har07。 har07的建議工作得很好。 – loksher