XDocument.AddAfterSelf isn’t creating correct XML structure

I use this code to save my data to XDocument:

var nameNode = stories.Descendants("name").Where(o => o.Value == loadedStory.name).First(); nameNode.AddAfterSelf(new XElement("readingTicks", loadedStory.readingTicks)) nameNode.AddAfterSelf(new XElement("categoryName", loadedStory.categoryName)); nameNode.AddAfterSelf(new XElement("link"), loadedStory.link); nameNode.AddAfterSelf(new XElement("meta", loadedStory.meta)); nameNode.AddAfterSelf(new XElement("description"), loadedStory.description); 

Then, when I go step-by-step in debug mode, the XDocument isn't showing up correctly.

Here's the output file:

<LibraryInstance xmlns:xsi="http://ift.tt/Atvu06; xmlns:xsd="http://ift.tt/FPH52k; <Stories> <FFStory> <name>Gol D Ann</name> <description />If your coming means your passing, should you be expected to cry or smile? [Ace with Twin fic]<meta>Rated: T - English - Adventure/Drama - Chapters: 3 - Words: 18,366 - Reviews: 21 - Favs: 36 - Follows: 48 - Updated: 4h - Published: 1/10 - Luffy, Ace, OC</meta><link />/s/10961658/1/Gol-D-Ann<categoryName /><readingTicks>0</readingTicks></FFStory> </Stories> </LibraryInstance> 

Not only the tags aren't closed correctly, but the formating is broken too. Is it because of the complexity of the string I pas as content? Are there any workarounds?

by MrReynevan2 via /r/csharp

Leave a Reply