繁体   English   中英

使用RestSharp解析格式错误的XML

[英]Parsing malformed XML with RestSharp

我有一个需要使用的Web服务返回了以下内容:

<catalog modules="2" children="0">
  <title>Test Catalog</title>
  <link type="application/xml" rel="self" href="http://someurl"/>
  <link type="text/html" rel="alternate" href="http://someurl"/>
  <parent modules="0" children="3">
    <title>Top</title>
    <link type="application/xml" rel="self" href="http://someurl"/>
    <link type="text/html" rel="alternate" href="http://someurl"/>
  </parent>
  <module>
    <id>MODULEID123</id>
    <title>Some module title</title>
    <link type="application/xml" rel="self" href="http://someurl"/>
    <link type="text/html" rel="alternate" href="http://someurl"/>
    <type>type123</type>
    <description>Some Description</description>
  </module>
  <module>
    <id>MODULEID456</id>
    <title>Some other module title</title>
    <link type="application/xml" rel="self" href="http://someurl"/>
    <link type="text/html" rel="alternate" href="http://someurl"/>
    <type>type123</type>
    <description/>
  </module>
</catalog>

我正在使用RestSharp来使用服务,在正常情况下,我希望标记位于父节点或类似节点的下面,以便我可以使用带有List<Module> Modules的响应类,该类将自动将其拉出但是,由于它们刚好等于<parent><title><link>节点,因此它看起来几乎是畸形的(尽管,自从我深入研究XML以来已经有很长时间了。 *应该*看起来-谢谢JSON!

因此,鉴于这是返回的结果,我如何指示RestSharp对此进行解析? 如果RestSharp期望格式正确的XML,因此拒绝了它,我是否应该使用老式的XMLReader手动解析它?

糟糕,在文档中找到了它。 显然,这是基于约定的,因此虽然看起来有些古怪,但如果我只是假设那里有一个父级正确构建了我的响应类,那应该没问题。

从文档...

XmlDeserializer

处理两种不同类型的列表:无父母(内联)和常规(嵌套)。 例如,以下两个XML结构

<?xml version="1.0" encoding="utf-8" ?>
<InlineListSample>
    <image src="1.gif">value1</image>
    <image src="2.gif">value2</image>
    <image src="3.gif">value3</image>
    <image src="4.gif">value4</image>
</InlineListSample>

<?xml version="1.0" encoding="utf-8" ?>
<NestedListSample>
    <images>
        <image src="1.gif">value1</image>
        <image src="2.gif">value2</image>
        <image src="3.gif">value3</image>
        <image src="4.gif">value4</image>
    </images>
</NestedListSample>

将映射到以下C#模式:

public class ListSample
{
    public List<Image> Images { get; set; }
}

public class Image
{
    public string Src { get; set; }
    public string Value { get; set; }
}

如果偶然两个元素结构都在同一文档中位于层次结构中的相关点,则以父级/嵌套/常规列表为准。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM