如何使用 XDocument 和 XElement 插入子标签?
How to insert a child tag using XDocument and XElement?
我有这个 xml 架构,我需要将子标签添加到:
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Entity id="123" action="add" date="04-Nov-2017"></Entity>
<Entity id="125" action="add" date="05-Nov-2017"></Entity>
</PFA>
预期结果是:
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Record>
<Entity id="123" action="add" date="04-Nov-2017"></Entity>
<Entity id="125" action="add" date="05-Nov-2017"></Entity>
</Record>
</PFA>
我试过这个代码:
var doc = XDocument.Load("data.xml");
var record = new XElement("Record");
doc.Element("PFA").Add(record);
doc.Save("data.xml");
但我的结果如下。它有结束标记,但缺少开始标记。
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Entity id="123" action="add" date="04-Nov-2017">
</Entity>
</Record>
</PFA>
已更新:
我用 Elements
尝试了这个解决方案:
var doc = XDocument.Load(file.FullName);
var record = new XElement("Record");
var pfa = doc.Element("PFA");
var entities = pfa.Elements("Entity");
entities.Remove(); // remove <Entity> from <PFA>
pfa.Add(record); // add <Record> to <PFA>
record.Add(entities); // add <Entity> to <Record>
doc.Save(file.FullName);
但仍然缺少开始记录标签,
输出变为:
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Record />
</PFA>
如果我使用Element
:
var doc = XDocument.Load(file.FullName);
var record = new XElement("Record");
var pfa = doc.Element("PFA");
var entities = pfa.Element("Entity");
entities.Remove(); // remove <Entity> from <PFA>
pfa.Add(record); // add <Record> to <PFA>
record.Add(entities); // add <Entity> to <Record>
doc.Save(file.FullName);
输出变为:
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Entity id="123" action="add" date="04-Nov-2017"></Entity></Record>
<Record><Entity id="123" action="add" date="04-Nov-2017"></Entity></Record>
<Record><Entity id="125" action="add" date="05-Nov-2017"></Entity></Record>
</PFA>
解决方法其实很简单。您从 PFA
标签中删除 Entity
标签,然后将新的 Record
标签添加到 PFA
标签并将 Entity
标签重新添加到新添加的 Record
标签。代码可能如下所示:
var pfa = doc.Element("PFA");
IList<XElement> entities = pfa.Elements("Entity").ToList();
foreach (var entity in entities)
{
entity.Remove(); // Remove all the <Entity> tags from <PFA>
}
var record = new XElement("Record");
pfa.Add(record); // Add the new <Record>
foreach (var entity in entities)
{
record.Add(entity); // Add the <Entity> tags back to <Record>
}
我有这个 xml 架构,我需要将子标签添加到:
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Entity id="123" action="add" date="04-Nov-2017"></Entity>
<Entity id="125" action="add" date="05-Nov-2017"></Entity>
</PFA>
预期结果是:
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Record>
<Entity id="123" action="add" date="04-Nov-2017"></Entity>
<Entity id="125" action="add" date="05-Nov-2017"></Entity>
</Record>
</PFA>
我试过这个代码:
var doc = XDocument.Load("data.xml");
var record = new XElement("Record");
doc.Element("PFA").Add(record);
doc.Save("data.xml");
但我的结果如下。它有结束标记,但缺少开始标记。
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Entity id="123" action="add" date="04-Nov-2017">
</Entity>
</Record>
</PFA>
已更新:
我用 Elements
尝试了这个解决方案:
var doc = XDocument.Load(file.FullName);
var record = new XElement("Record");
var pfa = doc.Element("PFA");
var entities = pfa.Elements("Entity");
entities.Remove(); // remove <Entity> from <PFA>
pfa.Add(record); // add <Record> to <PFA>
record.Add(entities); // add <Entity> to <Record>
doc.Save(file.FullName);
但仍然缺少开始记录标签,
输出变为:
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Record />
</PFA>
如果我使用Element
:
var doc = XDocument.Load(file.FullName);
var record = new XElement("Record");
var pfa = doc.Element("PFA");
var entities = pfa.Element("Entity");
entities.Remove(); // remove <Entity> from <PFA>
pfa.Add(record); // add <Record> to <PFA>
record.Add(entities); // add <Entity> to <Record>
doc.Save(file.FullName);
输出变为:
<?xml version="1.0" encoding="utf-8"?>
<PFA>
<Entity id="123" action="add" date="04-Nov-2017"></Entity></Record>
<Record><Entity id="123" action="add" date="04-Nov-2017"></Entity></Record>
<Record><Entity id="125" action="add" date="05-Nov-2017"></Entity></Record>
</PFA>
解决方法其实很简单。您从 PFA
标签中删除 Entity
标签,然后将新的 Record
标签添加到 PFA
标签并将 Entity
标签重新添加到新添加的 Record
标签。代码可能如下所示:
var pfa = doc.Element("PFA");
IList<XElement> entities = pfa.Elements("Entity").ToList();
foreach (var entity in entities)
{
entity.Remove(); // Remove all the <Entity> tags from <PFA>
}
var record = new XElement("Record");
pfa.Add(record); // Add the new <Record>
foreach (var entity in entities)
{
record.Add(entity); // Add the <Entity> tags back to <Record>
}