Question

I have run into a problem while trying to parse the response of a WebDAV application. The relevant part of the response looks like this: for collections:

        ....
        <D:getlastmodified xmlns:B="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" B:dt="dateTime.rfc1123">Tue, 15 Jan 2013 15:47:30 GMT</D:getlastmodified>
        <D:displayname>aaa.bc</D:displayname>
        <D:resourcetype>
           <D:collection />
        </D:resourcetype>
        <D:getcontenttype>text/html; charset=utf-8</D:getcontenttype>
        ....

for normal files:

        ....
        <D:getlastmodified xmlns:B="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" B:dt="dateTime.rfc1123">Tue, 15 Jan 2013 15:47:30 GMT</D:getlastmodified>
        <D:displayname>aaa.bc</D:displayname>
        <D:resourcetype />
        <D:getcontenttype>text/html; charset=utf-8</D:getcontenttype>
        ....

I want to parse this into a c# object with the property:

[XmlElement("resourcetype")]
public string Type {get;set;}

Where e.g. Type = "collection" for a collection.

How would I do this? For the part I posted my C# code looks like this (but does not do what I want):

    [XmlRoot("prop")]
    public class Prop
    {
        [XmlElement("creationdate")]
        public string CreationDate { get; set; }

        [XmlElement("getlastmodified")]
        public string LastModified { get; set; }

        [XmlElement("displayname")]
        public string DisplayName { get; set; }

        [XmlElement("resourcetype")]
        public string ResourceType { get; set; }

        [XmlElement("getcontenttype")]
        public string ContentType { get; set; }

        [XmlElement("getcontentlength")]
        public string ContentLength { get; set; }

        [XmlElement("getetag")]
        public string ETag { get; set; }

        [XmlElement("imagewidth")]
        public string ImageWidth { get; set; }

        [XmlElement("imageheight")]
        public string ImageHeight { get; set; }

        [XmlElement("thumbnailuri")]
        public string TumbnailUri { get; set; }

    }

    [XmlRoot("resourcetype")]
    public class ResourceType
    {
        [XmlElement("collection")] // TODO
        public string Collection { get; set; }
    }

and the method to parse everything:

 private T ParseWebDavXml<T>(string xml)
    {
        using (var reader = XmlReader.Create(new StringReader(xml)))
        {
            var serializer = new XmlSerializer(typeof(T), "DAV:");
            var result = (T)serializer.Deserialize(reader);
            return result;
        }
    }
Was it helpful?

Solution 2

You can change the string type to an object type for the ResourceType fied. You also need to decorate ResourceType fied with XmlType attributes. You specify on XmlType attribute by target (in your case, 2).

You also need to create two types :

  • One for the string,
  • One for the collection.

But you can use two more simpler solutions :

OTHER TIPS

Check this Link: I have tested this code Remove the FirtsColumn, SecondColumn from the code and utilize the xmlDS as per your requirements

XML Parsing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top