0

WebDAV アプリケーションの応答を解析しようとしているときに問題が発生しました。応答の関連部分は次のようになります: コレクションの場合:

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

通常のファイルの場合:

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

これを次のプロパティで ac# オブジェクトに解析したい:

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

たとえば、コレクションの場合は Type = "collection" です。

どうすればいいですか?私が投稿した部分については、C#コードは次のようになります (しかし、私が望むことはしません):

    [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; }
    }

すべてを解析する方法:

 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;
        }
    }
4

2 に答える 2

0

ResourceType フィールドの文字列型をオブジェクト型に変更できます。また、ResourceType フィールドを XmlType 属性で装飾する必要があります。ターゲットごとに XmlType 属性を指定します (この場合は 2)。

また、次の 2 つのタイプを作成する必要があります。

  • 1つは文字列用、
  • コレクションに1枚。

ただし、より単純な 2 つのソリューションを使用できます。

  • XSD と、この XSD に対応する C# クラスを生成します。 19359757#19359757]
  • 文字列フィールドの代わりにテーブルを直接定義する
于 2013-10-14T13:26:44.853 に答える