缺少xml根元素

时间:2013-12-19 16:33:26

标签: c# xml

很抱歉这个简单,但是这个是我的想法,hier是xml soap请求:

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <TarifResponse xmlns="http://www.april-technologies.com">
      <TarifResult>
        <Status>
          <Summary>
            <ResponseTechnicalLabel />
            <ResponseTechnicalData>OK</ResponseTechnicalData>
          </Summary>
        </Status>
        <BusinessData>
          <IdentifiantProjet>1296483</IdentifiantProjet>
          <Proposition>
            <PropositionOutput>
              <Ordre>1</Ordre>
              <Produit>SanteEssentiel</Produit>
              <Garantie>
                <GarantieOutput>
                  <Libelle>Niveau 1</Libelle>
                  <CotisationMensuelle>5998</CotisationMensuelle>
                </GarantieOutput>
              </Garantie>
              <ValiditeTarif>
                <DateDebut>01/01/2014</DateDebut>
                <DateFin>31/12/2014</DateFin>
              </ValiditeTarif>
              <OptionProduit />
            </PropositionOutput>

          </Proposition>
        </BusinessData>
      </TarifResult>
    </TarifResponse>
  </soap:Body>
</soap:Envelope>

和代码c#是

XDocument docxx = XDocument.Parse(Session["xmlrs" + z + ""].ToString());
            //This now holds the set of all elements named field

            try
            {
                XNamespace foobar = "http://www.april-technologies.com";
                var urlList = docxx.Descendants(foobar + "CotisationMensuelle")
                                      .Select(x => (string)x)
                                      .ToList();
                Console.WriteLine(urlList);




                rpMyRepeater1.DataSource = urlList;
                rpMyRepeater1.DataBind();
            }
            catch(Exception ex) {}

???我可以得到数据“CotisationMensuelle”???我得到空数据,侯ik可以解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

您可以使用Descendants方法,如下所示

XNamespace foobar = "http://www.april-technologies.com";
var urlList = docxx.Descendants(foobar + "CotisationMensuelle")
                      .Select(x => (string)x)
                      .ToList();
Console.WriteLine(urlList.Count);  

如果您尝试使用ElementElements,则必须使用完整的层次结构

XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace ns1 = "http://www.april-technologies.com";

var CotisationMensuelle=(string)xml.Root
                      .Element(soapenv + "Body")
                      .Element(ns1 + "TarifResponse")
                      .Element(ns1 + "TarifResult")
                      .Element(ns1 + "BusinessData")
                      .Element(ns1 + "Proposition")
                      .Element(ns1 + "PropositionOutput")
                      .Element(ns1 + "Garantie")
                      .Element(ns1 + "GarantieOutput")
                      .Element(ns1 + "CotisationMensuelle");