[英]Deserializing child elements in xml
How should I get the <site-standard-profile-request>
child element to deserialize correctly so that it does not show up as null? 我应该如何使
<site-standard-profile-request>
子元素正确反序列化,以使其不会显示为null?
The deserialization process is perfect; 反序列化过程是完美的; I just need to get the child element
<site-standard-profile-request>
to serialize as well. 我只需要获取子元素
<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();
You'll need another serializable class with just the url as a property. 您将需要另一个具有URL属性的可序列化类。 Eg,
例如,
[XmlRoot("site-standard-profile-request")]
[Serializable()]
public class StandardProfile
{
public string url { get;set;}
}
And then your existing class should use it, something like 然后您现有的类应使用它,例如
[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; }
}
I haven't tested this code, but should be pretty close. 我尚未测试此代码,但应该非常接近。
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.