你好,我有一个对象列表,我想转换成一个xml.这是最终的xml应该是什么样子.
<ArrayOfTweet>
<Tweet>
<Photos>
<Photo>
<PhotoHeight>FOO</PhotoHeight>
<PhotoUri>a/random/ur/path</PhotoUri>
<PhotoWidth>923</PhotoWidth>
<SourcePhotoUri>a/random/path</SourcePhotoUri>
</Photo>
</Photos>
<ProfileImage>a/random/path</ProfileImage>
<ScreenName>FOO</ScreenName>
<Text>some text</Text>
<TweetId>1234</TweetId>
<UserId>1234</UserId>
<Username>BAR</Username>
</Tweet>
<Tweet>
<Photos>
<Photo>
<PhotoHeight>FOO</PhotoHeight>
<PhotoUri>a/random/ur/path</PhotoUri>
<PhotoWidth>923</PhotoWidth>
<SourcePhotoUri>a/random/path</SourcePhotoUri>
</Photo>
</Photos>
<ProfileImage>a/random/path</ProfileImage>
<ScreenName>FOO</ScreenName>
<Text>some text</Text>
<TweetId>1234</TweetId>
<UserId>1234</UserId>
<Username>BAR</Username>
</Tweet>
</ArrayOfTweet>
Run Code Online (Sandbox Code Playgroud)
我已将列表中的每个对象转换为xml字符串,如此
//TweetList is the list of tweet objects
List<string> xmlStringTweetList = new List<string>();
foreach (var tl in TweetList)
{
xmlStringTweetList.Add(toXML(tl));
}
private string toXML(Tweet t)
{
var stringwriter = new System.IO.StringWriter();
var serializer = new XmlSerializer(t.GetType());
serializer.Serialize(stringwriter, t);
return stringwriter.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我尝试将该列表转换为上面使用的格式
XElement …Run Code Online (Sandbox Code Playgroud)