Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i need to convert all the empty XML nodes to self closed in a XML file.

like following:

XML
<colspec colnum="1" colname="col1" colwidth="*"></colspec>


should be

XML
<colspec colnum="1" colname="col1" colwidth="*"/>


I have tried the method
C#
XmlElement.IsEmpty = true;
but it returned with empty space before closing, like the following.
XML
<colspec colnum="1" colname="col1" colwidth="*" />


i have tried the below code:
C#
XmlDocument xDoc = new XmlDocument();
xDoc.PreserveWhitespace = true;
xDoc.Load(InputFile);

foreach (XmlElement xEle in xDoc.SelectNodes("descendant::*[not(node())]"))
{
    xEle.IsEmpty = true;
}
xDoc.Save(InputFile);


Please help in this regard.

Regards,
Kavin
Posted
Updated 23-Jun-13 19:45pm
v2
Comments
stibee 24-Jun-13 12:21pm    
I have no direct solution for you. But I hope the following link will help you:
http://blogs.msdn.com/b/ericwhite/archive/2009/07/08/empty-elements-and-self-closing-tags.aspx
kavin1989 25-Jun-13 2:10am    
I have tried this link already. But its work only for element doesn't contain any attributes.
savbace 25-Jun-13 10:04am    
Excuse me, but why you are trying to remove this space? It is common in self-closing XML elements.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900