【发布时间】:2010-11-19 08:22:26
【问题描述】:
我想根据我的要求插入迭代元素(信号),如下面的 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 文件的迭代元素?
【问题讨论】:
-
您的信号是否在某种集合中?
-
使用以下代码解决: for (int i = 0; i
标签: c# asp.net xml c#-3.0 linq-to-xml