1

<site-standard-profile-request>nullとして表示されないように、子要素を正しく逆シリアル化するにはどうすればよいですか?

デシリアライゼーション プロセスは完璧です。<site-standard-profile-request>子要素もシリアル化する必要があります。

 //<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
    //- <person>
    //  <first-name>Storefront</first-name> 
    //  <last-name>Doors</last-name> 
    //  <headline>CEO at StorefrontDoors.NET</headline> 
    //- <site-standard-profile-request>
    //  <url>http://www.linkedin.com/profile?viewProfile=&key=147482099&authToken=-Igm&authType=name&trk=api*a216630*s224617*</url> 
    //  </site-standard-profile-request>
    //  </person>
    [XmlRoot("person")]
    [Serializable()]
    public class LinkedIn
    {
        [XmlElement("first-name")]
        public string FirstName { get; set; }
        [XmlElement("last-name")]
        public string LastName { get; set; }
        [XmlElement("headline")]
        public string Headline { get; set; }
        public string URL { get; set; }
    }


 string profile = oauth.APIWebRequest("GET", "https://api.linkedin.com/v1/people/~", null);
        //

        LinkedIn lkIn = null;

        BufferedStream stream = new BufferedStream(new MemoryStream());
        stream.Write(Encoding.ASCII.GetBytes(profile), 0, profile.Length);
        stream.Seek(0, SeekOrigin.Begin);
        StreamReader sr = new StreamReader(stream);
        XmlSerializer serializer = new XmlSerializer(typeof(LinkedIn));

        lkIn = (LinkedIn)serializer.Deserialize(sr);
        stream.Close();
4

1 に答える 1

1

プロパティとして URL だけを持つ別のシリアル化可能なクラスが必要になります。例えば、

[XmlRoot("site-standard-profile-request")]
[Serializable()]
public class StandardProfile
{
    public string url { get;set;}
}

そして、既存のクラスはそれを使用する必要があります。

[XmlRoot("person")]
[Serializable()]
public class LinkedIn
{
    [XmlElement("first-name")]
    public string FirstName { get; set; }
    [XmlElement("last-name")]
    public string LastName { get; set; }
    [XmlElement("headline")]
    public string Headline { get; set; }

    public StandardProfile Profile { get;set; }
}

私はこのコードをテストしていませんが、かなり近いはずです。

それが役立つことを願っています。

于 2012-10-22T22:54:04.373 に答える