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