2013-12-19 93 views
0

很抱歉的简单,但是这一次是躲避箱,票数是XML SOAP请求:xml根元素的缺失

<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”?我得到空数据,hou ik可以解决这个问题吗?

+0

“CotisationMensuelle”里面GarantieOutput ......因此,像:根 - > TarifResult - > BusinessData - >命题 - > Garantie - > GarantieOutput - > CotisationMensualle – Crasher

+0

您可以使用XPath 2:http://www.w3schools.com/xpath/ ...并编写如下所示的内容:XmlDocument xmlDoc =新的Xm lDocument();加载... xmlDoc.SelectSingleNode(“//CotisationMensuelle").Value;或类似的东西=) – Crasher

回答

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"); 
+0

我测试了你的第一个代码感谢它的工作,但我有这个消息“[System.InvalidCastException] = {”无法转换类型'System.String'的对象来键入'System.Xml.XmlNode '。“}”当我把数据放入'rpMyRepeater1.DataSource = urlList; rpMyRepeater1.DataBind();''我的代码有什么问题? – user3030806

+0

你能用新代码更新问题吗? – Damith

+0

将'Label1.Text = urlList.ToString()'改为'Label1.Text = urlList.First()' – Damith