简体   繁体   中英

Parsing malformed XML with RestSharp

I have a webservice that I need to consume that's returning the following:

<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>

I'm using RestSharp to consume the service, and under normal circumstances I'd expect the tags to be underneath a parent node or something similar so that I could just a response class with a List<Module> Modules that would just automatically pull them in. However, since they're just out there equal to the <parent> , <title> and <link> nodes, it looks almost malformed (though, admittedly, it's been a long time since I've been deep into how XML *should * look - thank you, JSON!

So, given that this is the returned result, how can I direct RestSharp to parse this? If RestSharp expects well formed XML, and thus rejects this, should I just manually parse this using an XMLReader the old fashioned way?

Oops, found it in the documentation. Evidently this is convention-based, so while this looks a bit wonky, it should be no problem if I just assume there's a parent there build my response class correctly.

From the docs...

XmlDeserializer

Two different types of lists are handled: parentless (inline) and regular (nested). For instance, both of the following XML structures

<?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>

Will map to the following C# schema:

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

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

If by chance both element structures existed in the same document at the relevant point in the hierarchy, parented/nested/regular lists take priority.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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