如何使用 C#.net 在 XMl 文件中插入迭代元素?
我想根据我的要求插入迭代元素(信号),如下面的 xml 输出。
<?xml version="1.0" encoding="UTF-8"?>
<WIUConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Timestamp>2006-05-04T18:13:51.0Z</Timestamp>
<WIUAddress>WIUAddress0</WIUAddress>
<WIUName>WIUName0</WIUName>
<BeaconFlag>Y</BeaconFlag>
<EncryptedHMACkey>30</EncryptedHMACkey>
<DeviceStatusConfigVersion>DeviceStatusConfigVersion0</DeviceStatusConfigVersion>
<Signal>
<SiteDeviceId>SiteDeviceId0</SiteDeviceId>
<SiteName>SiteName0</SiteName>
<TrackName>TrackName0</TrackName>
</Signal>
<Signal>
<SiteDeviceId>SiteDeviceId1</SiteDeviceId>
<SiteName>SiteName1</SiteName>
<TrackName>TrackName1</TrackName>
</Signal>
<Signal>
.
.
.
</Signal>
</WIUConfig>
我如何使用 C#.net LINQ to XML 实现此迭代概念
这是我的代码:
XDocument xdco = new XDocument(
new XDeclaration("1.0", "utf-8", "Yes"),
new XComment("WIU Configurations"),
new XElement("WIUConfig",
new XElement("Timestamp", datetime),
new XElement("WIUAddress", ds.Tables[0].Rows[0][0].ToString()),
new XElement("WIUName", ds.Tables[0].Rows[0][1].ToString()),
new XElement("BeaconFlag", "Y"),
new XElement("EncryptedHMACkey", ds1.Tables[0].Rows[0][0].ToString()),
new XElement("DeviceStatusConfigSCAC", ds.Tables[0].Rows[0][0].ToString()),
new XElement("DeviceStatusConfigTableId", ds.Tables[0].Rows[0][0].ToString()),
new XElement("DeviceStatusConfigVersion", ds.Tables[0].Rows[0][0].ToString()),
**????????(iteration code) **
));
xdco.Save(OutPath);
从上面的代码如何使用 XML 文件插入迭代元素?
i want to insert iteration elements(Signal) according my requirement like below xml output.
<?xml version="1.0" encoding="UTF-8"?>
<WIUConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Timestamp>2006-05-04T18:13:51.0Z</Timestamp>
<WIUAddress>WIUAddress0</WIUAddress>
<WIUName>WIUName0</WIUName>
<BeaconFlag>Y</BeaconFlag>
<EncryptedHMACkey>30</EncryptedHMACkey>
<DeviceStatusConfigVersion>DeviceStatusConfigVersion0</DeviceStatusConfigVersion>
<Signal>
<SiteDeviceId>SiteDeviceId0</SiteDeviceId>
<SiteName>SiteName0</SiteName>
<TrackName>TrackName0</TrackName>
</Signal>
<Signal>
<SiteDeviceId>SiteDeviceId1</SiteDeviceId>
<SiteName>SiteName1</SiteName>
<TrackName>TrackName1</TrackName>
</Signal>
<Signal>
.
.
.
</Signal>
</WIUConfig>
how i can achive this iteration concepts using C#.net LINQ to XML
Here is my Code:
XDocument xdco = new XDocument(
new XDeclaration("1.0", "utf-8", "Yes"),
new XComment("WIU Configurations"),
new XElement("WIUConfig",
new XElement("Timestamp", datetime),
new XElement("WIUAddress", ds.Tables[0].Rows[0][0].ToString()),
new XElement("WIUName", ds.Tables[0].Rows[0][1].ToString()),
new XElement("BeaconFlag", "Y"),
new XElement("EncryptedHMACkey", ds1.Tables[0].Rows[0][0].ToString()),
new XElement("DeviceStatusConfigSCAC", ds.Tables[0].Rows[0][0].ToString()),
new XElement("DeviceStatusConfigTableId", ds.Tables[0].Rows[0][0].ToString()),
new XElement("DeviceStatusConfigVersion", ds.Tables[0].Rows[0][0].ToString()),
**????????(iteration code) **
));
xdco.Save(OutPath);
from above code how to insert iterate element with XML file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还没有显示信号数据方面的内容,但是您应该能够在最终现有的
new XElement
行之后直接执行类似的操作:LINQ to XML 足够聪明,可以对可迭代的参数进行递归。这是它与 LINQ 的其余部分很好地集成的方式之一。
编辑:从您的评论来看,您已经拥有数据,但在数据表中。您仍然可以使用 DataTable.AsEnumerable().Select(row => ...) 应用相同的方法,但我个人强烈考虑首先将其转换为强类型集合,以保留代码简单且可维护。
You haven't shown what you've got in terms of signal data, but you should be able to do something like this, directly after your final existing
new XElement
line:LINQ to XML is clever enough to recurse over parameters which turn out to be iterable. It's one of the ways it integrates very nicely with the rest of LINQ.
EDIT: Judging by your comments, you already have the data but in a DataTable. You could still apply the same approach using
DataTable.AsEnumerable().Select(row => ...)
but personally I'd strongly consider converting it into a strongly typed collection first, to keep the code simple and maintainable.您可以从中创建“业务对象”的中间集合,然后仅使用 DataContractSerializer 对其进行序列化。
You can create an intermediate collection of "Business objects" out of it and then just use the DataContractSerializer to serialize it.