I'm trying to filter out empty font nodex from an HMTL doc that has been loaded into an XML doc.
My XML looks like this:
<root>
<font>
<font color="#141414">
<font>
<font></font>
</font><font></font><font> </font>(I need this text in the outer font)
</font>
</font>
</root>
and I need it to look like this:
<root>
<font color="#141414"> (I need this text in the outer font)</font>
</root>
Note the space from the inner font node has also been moved.
I wrote a console app using a recursive function found on SO but it doesn't work. I can't use Linq to XML nor any third party libraries. I'm a bit lost.
This was my console app:
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root><font><font color=\"#141414\"><font><font></font></font><font></font><font> </font>(I need this text in the outer font)</font></font></root>");
XmlNode node = doc.SelectSingleNode("root");
RemoveEmptyFontTags(node.ChildNodes);
}
private static void RemoveEmptyFontTags(XmlNodeList nodes)
{
foreach(XmlNode node in nodes)
{
RemoveEmptyFontTags(node.ChildNodes);
if(node.Name.ToLower() == "font" && node.Attributes.Count == 0)
{
node.ParentNode.InnerXml = node.InnerXml;
}
}
}
}
No comments:
Post a Comment