【问题标题】:xml root element missingxml 根元素丢失
【发布时间】:2013-12-19 16:33:26
【问题描述】:

很抱歉,这很简单,但这个是我躲避的,它是 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”吗???我得到空数据,我能解决这个问题吗?

【问题讨论】:

  • “CotisationMensuelle”在 GarantieOutput 里面...所以类似于:Root -> TarifResult -> BusinessData -> Proposition -> Garantie -> GarantieOutput -> CotisationMensualle
  • 您可以使用 XPath 2:w3schools.com/xpath ...并编写如下内容: XmlDocument xmlDoc = new XmlDocument();加载... xmlDoc.SelectSingleNode ("//CotisationMensuelle").Value;或类似的东西 =)

标签: c# xml


【解决方案1】:

你可以使用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");

【讨论】:

  • 我已经测试了你的第一个代码谢谢它的工作,但我有这个消息“[System.InvalidCastException] = {“无法将'System.String'类型的对象转换为'System.Xml'。 XmlNode'."}" 当我将数据放入rpMyRepeater1.DataSource = urlList; rpMyRepeater1.DataBind();????我的代码有什么问题?
  • Label1.Text=urlList.ToString()更改为Label1.Text=urlList.First()
  • 完整消息是 System.InvalidCastException:无法将“System.Char”类型的对象转换为“System.Xml.XmlNode”类型。在 System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) 在 System.Web.UI.WebControls.Repeater.DataBind() 在 aprilService.Page_Load(Object sender, EventArgs e) c:\WebSite1\WebSite1\aprilService。 aspx.cs:line 122 'rpMyRepeater1.DataBind();'`
  • 这是中继器绑定问题,你最好用中继器绑定相关代码提出新问题
猜你喜欢
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-19
  • 2015-12-20
  • 2021-08-20
  • 2019-02-12
相关资源
最近更新 更多