簡體   English   中英

嘗試替換xml節點值

[英]Trying to replace xml node value

我試圖將節點值從123456789替換為100200525。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>bronze@test.com</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>

解決方案,我嘗試了哪些dint工作:

//開始

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

需要幫忙

您的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM