0

how to remove the parent tag from the xml " Tickets" the remaining nodes should be there

<Tickets>
    <Extract_Date>2011-02-25 00:00:00</Extract_Date>
    <Ticket>
        <Ticket_Number>INC000000578057</Ticket_Number>
        <Status>
            <Value>Cancelled</Value>
            <Reason>Cancelled by user</Reason>
        </Status>
    </Ticket>
</Tickets>
Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58

2 Answers2

3

The .Elements() method of XElement returns all of the element's children.

IEnumerable<XElement> GetElementsWithoutParent(XElement element)
{
    return element.Elements();
}

Of course, such a method would be silly when you could just call the .Elements() method directly, but it illustrates the point.

Keep in mind that you no longer have an XElement, but an XElement collection. You can no longer get the XML representation by simply calling .ToString(). To get the XML, you'll need to use a StringBuilder and append each individual element. And it can't represent an XML document without a root node.

gilly3
  • 87,962
  • 25
  • 144
  • 176
  • return element.Elements();it just returns null, dont know why it does that –  Jul 12 '12 at 22:29
  • @Gani - Please include your C# code in your question. – gilly3 Jul 12 '12 at 22:32
  • i have to serialize the xml without parent otherwise it throws error while deserializing –  Jul 12 '12 at 22:40
  • @Gani - Please include your C# code in your question. Click "Edit" under your question, then add some C# code and save changes. – gilly3 Jul 12 '12 at 22:54
2

want to serialize the other nodes and save it to DB,while deserializing adding dynamically the parent

XElement newRoot = ........
newRoot.Add(orgXDoc.Element("Tickets").Elements());
L.B
  • 114,136
  • 19
  • 178
  • 224