4

Hello I have a list of object i want to convert into an xml. Here is what the final xml should look like.

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

I have converted each of the objects in the list into an xml string like so

//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();
}

I tried converting that list into the format above using

XElement xmlElements = new XElement("ArrayOfTweet", xmlStringTweetList.Select(i => new XElement("Tweet", i)));

But there is the extra <Tweet></Tweet> That i dont need. Is there a way of doing this?

4
  • Why aren't you taking an entire object structure and serializing all at once? Commented Jun 8, 2017 at 18:31
  • You mean serialize the TweetList object? I tried that but i could not figure out how to make that work properly. I getting errors. Do you have a suggestion of a way to do that? I would be happy to do it that way, since it would be easier Commented Jun 8, 2017 at 18:33
  • I just posted an answer with how to serialize it as one object. Commented Jun 8, 2017 at 18:44
  • The types in square bracket are wrong. Take out the [XmlArray] above Tweet and replace with [XmlElement]. Commented Jun 8, 2017 at 18:54

2 Answers 2

5

I made a fiddle here that illustrates a way to serialize your object all at once, instead of piecing strings together.

I suspect your extra <Tweet></Tweet> is because of a null or empty value in the list, because I am not experiencing it in my test above.

Sign up to request clarification or add additional context in comments.

Comments

1

I think XElement xmlElements = new XElement("ArrayOfTweet", xmlStringTweetList.Select(i => XElement.Parse(i))); should do.

3 Comments

Thanks @Martin, I get an error System.Xml.XmlElement does not contain a definition for 'Parse' but i check here msdn.microsoft.com/en-us/library/bb468714(v=vs.110).aspx and it is there. ill have to figure out why this is
Sorry, it should be XElement.Parse.
Thanks Martin i made the changes and it worked too! But i will go with @maccetturas fiddle because it is clean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.