我有非常大的xml数据集。因此"SECTION"
包含"Piece"
节点,"SECTION name="RF1WB-1"
,SECTION name="RF1WB-2"
,SECTION name="RF1WB-3"
的值相等。因此,如果"Parent"
标记包含与第二个XML中所示相同的值,我希望将"Piece"
添加为父节点。
如果子节点元素包含相同的值,如何添加父节点?
XmlDocument doc = new XmlDocument();
doc.Load("..\\MFAB1.xml");
XmlNodeList xnList = doc.SelectNodes("MFAB.ini/SECTION");
foreach (XmlNode xn in xnList)
{
if (xn.HasChildNodes)
{
foreach (XmlNode item in xn.SelectNodes("Piece"))
{
if (xn.ChildNodes[0].InnerText.ToString().Contains(s3))
{
d1.Piece = xn.ChildNodes[0].InnerText.ToString();
}
______________
______________
______________
}
}
}
输入文件:
<?xml version="1.0" encoding="UTF-8"?>
<MFAB.ini>
<SECTION name="RF1WB-1">
<Piece>
RF1-1
</Piece>
</SECTION>
<SECTION name="RF1WB-2">
<Piece>
RF1-1
</Piece>
</SECTION>
<SECTION name="RF1WB-3">
<Piece>
RF1-1
</Piece>
</SECTION>
<SECTION name="RF1-2WB-1">
<Piece>
RF1-2
</Piece>
</SECTION>
<SECTION name="RF1-2WB-2">
<Piece>
RF1-2
</Piece>
</SECTION>
</MFAB.ini>
预期的输出文件:
我需要的是:
<?xml version="1.0" encoding="UTF-8"?>
<MFAB.ini>
<Parent name = "RF1-1">
<SECTION name="RF1-1WB-1">
<Piece>
RF1-1
</Piece>
</SECTION>
<SECTION name="RF1-1WB-2">
<Piece>
RF1-1
</Piece>
</SECTION>
<SECTION name="RF1-1WB-3">
<Piece>
RF1-1
</Piece>
</SECTION>
</Parent>
<Parent name = "RF1-2">
<SECTION name="RF1-2WB-1">
<Piece>
RF1-2
</Piece>
</SECTION>
<SECTION name="RF1-2WB-2">
<Piece>
RF1-2
</Piece>
</SECTION>
</Parent>
</MFAB.ini>
答案 0 :(得分:2)
您可以按<Piece>
标记对元素进行分组,然后以这种方式将其作为<Parent>
标记的子代:
var xml = XDocument.Load(@"input file");
var result = new XElement(xml.Root.Name, xml.Root.Attributes().ToArray(),
xml.Root.Elements().GroupBy(x => x.Element("Piece").Value.Trim()).Select(x =>
new XElement("Parent", new XAttribute("name", x.Key), x))
);
result.Save(@"output file");
答案 1 :(得分:1)
为了让自己更容易,您可以使用XmlSerializer
来阅读您的数据,并且您可以为您的Xml定义一个dto,如此
[XmlRoot("MFAB.ini")]
public class Mfab {
[XmlElement("SECTION")]
public Section[] Sections { get; set; }
}
public class Section {
[XmlAttribute("name")]
public string Name { get; set; }
[XmlElement("Piece")]
[XmlText]
public string Piece { get; set; }
}
这将按原样读取您的文档,将您的所有部分作为Mfab课程的一部分(抱歉,我不知道这些课程的更好名称;)
这个Xml,你可以这样阅读
private static T GetXmlFromFile<T>(string filename) {
XmlSerializer xs = new XmlSerializer(typeof(T));
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
return (T)xs.Deserialize(fs);
}
}
哪个会给你一个包含所有部分的类。现在,既然你想重新格式化你的数据,你应该创建一个新的类来保存父节,然后创建一个新的xml根来保存它们
[XmlRoot("MFAB.ini")]
public class MfabWithParents {
[XmlElement("PARENT")]
public ParentSection[] Parents { get; set; }
}
public class ParentSection {
[XmlAttribute("name")]
public string Name { get; set; }
[XmlElement("SECTION")]
public Section[] Sections { get; set; }
}
ParentSection
仍会重复使用之前创建的Section
类。
现在要进行转换,我们可以实现以下步骤
// get the xml from a file
var source = GetXmlFromFile<Mfab>(Path.Combine(Environment.CurrentDirectory, "data.xml"));
// group all the sections by it's Piece
var groupedSections = source.Sections.GroupBy(section => section.Piece);
// convert the sections from the Grouped section to a parent section with name as attribute
var parentSections = groupedSections.Select(grp => new ParentSection
{
Name = grp.Key,
Sections = grp.ToArray()
}).ToArray();
// save the xml to a new file (which could now be read with the MfabWithParents class)
SaveToXml(new MfabWithParents { Parents = parentSections }, Path.Combine(Environment.CurrentDirectory, "data2.xml"));
然后保存方法看起来像这样
private static void SaveToXml(object data, string filename) {
XmlSerializer xs = new XmlSerializer(data.GetType());
using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) {
xs.Serialize(fs, data);
}
}