【问题标题】:Get pseudo xml inside xml structure在xml结构中获取伪xml
【发布时间】:2015-04-21 08:15:14
【问题描述】:

我有一些第三方 xml,我正在尝试解析。

这个问题类似于this one,因为我正在寻找隐藏在其中一个元素中的伪 xml 代码。但是,我需要的结果是不同的。

这是返回的 xml:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <PostApplication_V6Response xmlns="http://xxxService.org/">
          <PostApplication_V6Result>string</PostApplication_V6Result>
        </PostApplication_V6Response>
      </soap:Body>
    </soap:Envelope>

我正在使用 Linq to XML - 我可以返回元素 &lt;PostApplication_V6Result&gt; - 这是我可以检索到的树中最低的元素。

使用此代码:

    var name = "{http://xxxService.org/}PostApplication_V6Result";

    var soap = XDocument.Parse(result)
        .Descendants(name)
        .First();

但是,该元素中包含的值是某种伪 xml - 不是 xml,而是类似 xml。

这是里面的内容:

<xxxService>
    <Application>
        <Status>Accepted</Status>
        <RedirectUrl>http://www.google.com?abc=123</RedirectUrl>
        <Value>100</Value>
    </Application>
</xxxService>

我已经尝试了几乎所有方法来获取数据,但我收到了无效字符“=”错误或根无效消息的数据。

理想情况下,我希望将包括“应用程序”节点中的数据置于可以通过如下所示的通用解析器运行它的状态,但如果我必须手动执行某些操作,我会这样做。这几天我一直在尝试解决这个问题。

public static T Deserialise<T>(this XElement element)
{
    var serializer = new XmlSerializer(typeof(T));

    using (var reader = element.CreateReader())
    {
        return (T)serializer.Deserialize(reader);
    }
}

任何帮助表示赞赏。

更新

这是返回的完整 xml - 正如您所看到的,内部实际上是 html 而不是 xml。

<soap:body><postapplication_v6response xmlns="http://xxxService.org/"><postapplication_v6result>&lt;xxxService&gt;
&lt;Application&gt;
&lt;Status&gt;PURCHASED&lt;/Status&gt;
&lt;RedirectURL&gt;http://www.google.com?test=abc&amp;xyz=123&lt;/RedirectURL&gt;
&lt;/Application&gt;
&lt;/xxxService&gt;
</postapplication_v6result></postapplication_v6response></soap:body></soap:envelope>

【问题讨论】:

  • 你能发布完整的xml吗?听起来您的服务没有返回有效的 xml?
  • 如果节点的内容是一个包含 xml 字符的字符串,你应该将它包装在 [CData] 中或转义它
  • @Ewan - 添加了代码 Ewan - 如您所见,内部部分是 html,而不是 xml。
  • 好吧,只要使用 innerText 并像这个答案stackoverflow.com/questions/2203485/…
  • @Ewan - 当我尝试重新解析它时,我仍然对“=”是数据感到困惑。我这样做是为了到达应用程序节点。

标签: c# xml linq linq-to-xml


【解决方案1】:

这是一个例子。 (我已经取出了命名空间):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject2
{
    [TestClass]
    public class Class7
    {
        [TestMethod]
        public void xmltest()
        { 
            string xml = @"<body><postapplication_v6response><postapplication_v6result>&lt;xxxService&gt;
&lt;Application&gt;
&lt;Status&gt;PURCHASED&lt;/Status&gt;
&lt;RedirectURL&gt;http://www.google.com?test=abc&amp;xyz=123&lt;/RedirectURL&gt;
&lt;/Application&gt;
&lt;/xxxService&gt;
</postapplication_v6result></postapplication_v6response></body>";

            XDocument doc = XDocument.Parse(xml);
            string encodedhtml = doc.Descendants("postapplication_v6result")
                    .First().Value;

            string decodedhtml = HttpUtility.HtmlDecode(encodedhtml);

            Console.WriteLine(decodedhtml);
        }
    }
}

【讨论】:

  • Ewan,请看我的最后一个答案 - 我有结果,但它需要解析自己才能使用。
  • 你需要一个 html 解析器库。我看看
【解决方案2】:

解码整个字符串的副作用是,一些需要保持编码的 XML 特殊字符(在这种情况下为 &amp;amp; char),它们被解码导致无效的 XML。对于这个简单的情况,用&amp;amp; 替换&amp;amp; 应该可以解决它:

var xml = @"<PostApplication_V6Result>
&lt;xxxService&gt;
&lt;Application&gt;
&lt;Status&gt;PURCHASED&lt;/Status&gt;
&lt;RedirectURL&gt;http://www.google.com?test=abc&amp;xyz=123&lt;/RedirectURL&gt;
&lt;/Application&gt;
&lt;/xxxService&gt;
</PostApplication_V6Result>";
var soap = XElement.Parse(xml);

var rawContent = HttpUtility.HtmlDecode(soap.FirstNode.ToString().Trim())
                            .Replace("&", "&amp;");
var content = XElement.Parse(rawContent);

如果需要,修改代码以编码other XML special characters

【讨论】:

  • 感谢你们俩的帮助 - har07,你得到了有效的最终完整答案,但 Ewan 你也得到了我的支持。
  • 嗯。我担心带有&符号的 xml != html 已转义。但为什么首先是 html?!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
相关资源
最近更新 更多