2

我有一个现有的 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>
4

1 回答 1

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>

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

于 2013-11-14T21:01:21.140 回答