尝试替换xml节点值

时间:2014-04-16 23:31:37

标签: c# .net xml linq

我试图将节点值从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>

解决方案我尝试了哪种工作:

//启动

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

//结束

我也尝试过Linq。当我提到rootnode时,我试图达到&#34; EmpId&#34;通过上面的层次结构节点

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

需要帮助

1 个答案:

答案 0 :(得分: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);