This operation would create an incorrectly structured document. Xdocument

Asked

Viewed 79 times

0

I’m trying to create an XML, but I’m not getting it. I want it to have this format:

<item>
      <linktext></linktext>
      <etc..></etc...>
</item>

I can add one item, but when I go back through the loop, it will come out of the try and give me the title error. What am I doing wrong? Here’s how I create XML:

XDocument xmlOut = new XDocument();
 XElement item = new XElement("item",
                        new XElement("linktext", processedXML),
                        new XElement("link", "http://www.boe.es/diario_boe/txt.php?id=" + tmpName[1]),
                        new XElement("guid", newPdf.ToString()),
                        new XElement("pubDate", DateTime.Now)
                        );

                    xmlOut.Add(item);

1 answer

0

The problem was that I was trying to add several elements to the root of XML, which is wrong (from what I noticed after a few searches). What I did was create a XElement <itens> and then instead of:

xmlOut.Add(item);

used

xmlOut.Root.Element("itens").Add(item);

and the problem was solved, having several <item> inside <itens>, as intended.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.