如何折叠空的xml标签?

时间:2021-12-29 06:21:10

I'm getting XML like this:

我得到这样的XML:

<Items> <Row attr1="val"></Row> <Row attr1="val2"></Row> </Items>

This is valid XML, as you know, but another library I'm using is busted and it will only accept XML in this format:

如您所知,这是有效的XML,但我正在使用的另一个库已被破坏,它只接受以下格式的XML:

<Items> <Row attr1="val"/> <Row attr1="val2"/> </Items>

I'm already reading the XML into XmlDocuments, manipulating them, and rewriting them using an XmlWriter(), what's the easiest (and most efficient) way for me to "collapse" these empty tags?

我已经将XML读入XmlDocuments,操纵它们,并使用XmlWriter()重写它们,我最简单(也是最有效)的方法是“折叠”这些空标记?

5 个解决方案

#1


4  

Set the IsEmpty property of each XmlElement you want to collapse to true.

将要折叠的每个XmlElement的IsEmpty属性设置为true。

#2


0  

If you use System.XML's DOM manipulation objects (XmlElement etc) instead of XmlWriter, you get this for free.

如果你使用System.XML的DOM操作对象(XmlElement等)而不是XmlWriter,你可以免费获得它。

XmlElement items = xmlDoc.SelectNodes("items");
XmlElement row = xmlDoc.CreateElement("row");
items[0].appendChild(row);

You'll get a "<row/>"

你会得到一个“

#3


0  

You can try this.

你可以试试这个。

Subclass XmlTextWriter with an implementation whose WriteFullEndElement calls base.WriteEndElement. Like this:

子类XmlTextWriter,其实现的WriteFullEndElement调用base.WriteEndElement。像这样:

    public class BetterXmlTextWriter : XmlTextWriter
    {
        public BetterXmlTextWriter(TextWriter w)
            : base(w)
        {
        }

        public override void WriteFullEndElement()
        {
            base.WriteEndElement();
        }
    }

Then write the document out to an instance of your subclass using XmlDocument.WriteContentTo.

然后使用XmlDocument.WriteContentTo将文档写入子类的实例。

#4


0  

If you're already using an XmlWriter to write your XML, it should already be collapsing empty elements. This code (using .Net 3.5):

如果您已经在使用XmlWriter编写XML,那么它应该已经折叠了空元素。此代码(使用.Net 3.5):

XmlWriter xw = XmlWriter.Create(Console.Out);
xw.WriteStartElement("foo");
xw.WriteAttributeString("bar", null, "baz");
xw.WriteEndElement();
xw.Flush();
xw.Close();

emits <foo bar='baz' />.

发出

If your XmlWriter isn't, you should check to make sure that your XmlWriter code isn't emitting text nodes that you aren't expecting.

如果你的XmlWriter不是,你应该检查以确保你的XmlWriter代码没有发出你不期望的文本节点。

#5


0  

Here's a recursive method:

这是一个递归方法:

private static void FormatEmptyNodes(XmlNode rootNode)
{
    foreach (XmlNode childNode in rootNode.ChildNodes)
    {
        FormatEmptyNodes(childNode);

        if(childNode is XmlElement)
        {
            XmlElement element = (XmlElement) childNode;
            if (string.IsNullOrEmpty(element.InnerText)) element.IsEmpty = true;
        }
    }
}

Used like this:

像这样使用:

var doc = new XmlDocument();
doc.Load(inputFilePath);
FormatEmptyNodes(doc);
doc.Save(outputFilePath);

#1


4  

Set the IsEmpty property of each XmlElement you want to collapse to true.

将要折叠的每个XmlElement的IsEmpty属性设置为true。

#2


0  

If you use System.XML's DOM manipulation objects (XmlElement etc) instead of XmlWriter, you get this for free.

如果你使用System.XML的DOM操作对象(XmlElement等)而不是XmlWriter,你可以免费获得它。

XmlElement items = xmlDoc.SelectNodes("items");
XmlElement row = xmlDoc.CreateElement("row");
items[0].appendChild(row);

You'll get a "<row/>"

你会得到一个“

#3


0  

You can try this.

你可以试试这个。

Subclass XmlTextWriter with an implementation whose WriteFullEndElement calls base.WriteEndElement. Like this:

子类XmlTextWriter,其实现的WriteFullEndElement调用base.WriteEndElement。像这样:

    public class BetterXmlTextWriter : XmlTextWriter
    {
        public BetterXmlTextWriter(TextWriter w)
            : base(w)
        {
        }

        public override void WriteFullEndElement()
        {
            base.WriteEndElement();
        }
    }

Then write the document out to an instance of your subclass using XmlDocument.WriteContentTo.

然后使用XmlDocument.WriteContentTo将文档写入子类的实例。

#4


0  

If you're already using an XmlWriter to write your XML, it should already be collapsing empty elements. This code (using .Net 3.5):

如果您已经在使用XmlWriter编写XML,那么它应该已经折叠了空元素。此代码(使用.Net 3.5):

XmlWriter xw = XmlWriter.Create(Console.Out);
xw.WriteStartElement("foo");
xw.WriteAttributeString("bar", null, "baz");
xw.WriteEndElement();
xw.Flush();
xw.Close();

emits <foo bar='baz' />.

发出

If your XmlWriter isn't, you should check to make sure that your XmlWriter code isn't emitting text nodes that you aren't expecting.

如果你的XmlWriter不是,你应该检查以确保你的XmlWriter代码没有发出你不期望的文本节点。

#5


0  

Here's a recursive method:

这是一个递归方法:

private static void FormatEmptyNodes(XmlNode rootNode)
{
    foreach (XmlNode childNode in rootNode.ChildNodes)
    {
        FormatEmptyNodes(childNode);

        if(childNode is XmlElement)
        {
            XmlElement element = (XmlElement) childNode;
            if (string.IsNullOrEmpty(element.InnerText)) element.IsEmpty = true;
        }
    }
}

Used like this:

像这样使用:

var doc = new XmlDocument();
doc.Load(inputFilePath);
FormatEmptyNodes(doc);
doc.Save(outputFilePath);