【发布时间】:2018-08-23 10:30:45
【问题描述】:
我有一个表,其中 xml 数据类型的列有许多以下格式的 xml。
<Hash HashValue = "9738839398923">
<Element1>
<childelement1 attribute1 = "..." attribute2 = "...">
<childelement2 attribute1 = "..." attribute2 = "...">
<childelement3 attribute1 = "..." attribute2 = "...">
</Element1>
<Element2>
......
......
</Element2>
</Hash>
现在,我想从表中获取所有这些行并合并它们。我已经完成了获取部分并将其存储在数据表中。
为了合并,我在这个网站上使用了不同的解决方案,但我无法得到令人满意的结果。
预期输出格式:
<Hash HashValue = "4972904980">
.......
......
</Hash>
<Hash HashValue = "4534543">
.......
......
</Hash>
<Hash HashValue = "536456456456">
.......
......
</Hash>
到目前为止我得到的最接近的是:
<Hash HashValue = "4972904980">
<Hash HashValue = "4534543">
.......
......
</Hash>
</Hash>
以上输出代码:
FileStream file = File.Open(fakePath, FileMode.Create);
XElement xFileRoot = XElement.Parse(dt.Rows[0]["ColumnName"].ToString());
XElement xFileChild = XElement.Parse(dt.Rows[1]["ColumnName"].ToString());
xFileRoot.Add(xFileChild);
xFileRoot.Save(file);
上面的代码清楚地将第二个 xml 视为第一个的子级,这显然不是我的意图。
如何达到我的预期输出?
【问题讨论】:
-
尝试以下操作: XElement root = new XElement("root", dt.AsEnumerable().Select(x => XElement.Parse(x.Field
("ColumnName")))) ;
标签: c# .net xml linq-to-xml