使用选项卡格式化XElement

时间:2013-11-14 14:56:40

标签: c# linq-to-xml

我有一个现有的xml文档:

<?xml version="1.0" encoding="utf-16"?>
<property_set_list>
    <property_set symbol_id="TestPropertySet1">
        <property symbol_id="TestName1" id="1" type="8">Test</property>
        <property symbol_id="TestName2" id="2" type="8"></property>
    </property_set>
    <property_set symbol_id="TestPropertySet2">

    </property_set>
</property_set_list>

我加载它并更新并添加一些属性。例如:

TestName1属性已更新。  TestName 2属性保持不变。

在TestPropertySet2中,我添加了一些新属性。

所有操作之后的文档输出如下:

<?xml version="1.0" encoding="utf-16"?>
<property_set_list>
    <property_set symbol_id="TestPropertySet1">
        <property symbol_id="TestName1" id="1" type="8">qwerty</property>
        <property symbol_id="TestName2" id="1" type="8">
        </property>
    </property_set>
    <property_set symbol_id="TestPropertySet2">
        <property symbol_id="localeID" id="1" type="19">1033</property>
        <property symbol_id="localeID" id="2" type="19">1079</property>
    </property_set>
</property_set_list>

这是我用来加载和操作文档的代码:

using (var reader = ReaderCreator())
            using (var output = OutputCreator())
            using (var xmlWriter = new XmlTextWriter(output))
            {
                xmlWriter.Indentation = 1;
                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.IndentChar = TAB;

                XDoc = XElement.Load(reader, LoadOptions.None);
                Namespace = "some namespace";

               // some code to update properties


                // adds new nodes to TestPropertySet2
                foreach (var localeId in _franchise.LocaleIds)
                {
                    TestPropertySet2.Add(
                        new XElement(
                            Namespace + "property", localeId,
                            new XAttribute("symbol_id", "localeID"),
                            new XAttribute("id", i),
                            new XAttribute("type", "19")
                        )
                    );
                    i++;
                }

                XDoc.Save(xmlWriter);
            }

问题是TestName2属性现在有两行:

<property symbol_id="TestName2" id="1" type="8">
            </property>

1 个答案:

答案 0 :(得分:4)

我不知道您的ReaderCreator()OutputCreator()方法是做什么的。但是下面的代码按照我的要求在我这边工作。

string xml = "<property_set_list><property_set symbol_id=\"TestPropertySet1\"><property symbol_id=\"TestName1\" id=\"1\" type=\"8\">qwerty</property>" +
    "<property symbol_id=\"TestName2\" id=\"1\" type=\"8\"></property></property_set><property_set symbol_id=\"TestPropertySet2\">" +
    "<property symbol_id=\"localeID\" id=\"1\" type=\"19\">1033</property><property symbol_id=\"localeID\" id=\"2\" type=\"19\">1079</property></property_set></property_set_list>";

XElement xdoc = XElement.Parse(xml);
XElement TestPropertySet2 = xdoc.Elements()
    .Where(x => x.Attribute("symbol_id").Value == "TestPropertySet2")
    .FirstOrDefault();

TestPropertySet2.Add(
    new XElement(
       "property", "locale_id_val",
       new XAttribute("symbol_id", "localeID"),
       new XAttribute("id", "99"),
       new XAttribute("type", "19")
       ));

XmlWriterSettings xws = new XmlWriterSettings();
xws.Indent = true;
xws.IndentChars = "\t\t";

FileStream fs = new FileStream("test.xml", FileMode.Create);
using (XmlWriter xw = XmlWriter.Create(fs, xws))
{
     xdoc.Save(xw);
}
fs.Close();

这会产生以下输出(缩进保持):

<property_set_list>
    <property_set symbol_id="TestPropertySet1">
            <property symbol_id="TestName1" id="1" type="8">qwerty</property>
            <property symbol_id="TestName2" id="1" type="8"></property>
    </property_set>
    <property_set symbol_id="TestPropertySet2">
            <property symbol_id="localeID" id="1" type="19">1033</property>
            <property symbol_id="localeID" id="2" type="19">1079</property>
            <property symbol_id="localeID" id="99" type="19">locale_id_val</property>
    </property_set>

请勿使用XmlWriter代替XmlTextWriter。我不知道这是不是你问题的原因。此代码可以很容易地适应您的特定实现。