如何使用C#.net在XMl文件中插入迭代元素?
我想根据我的需求插入迭代元素(信号),如下面的xml输出如何使用C#.net在XMl文件中插入迭代元素?,c#,asp.net,xml,c#-3.0,linq-to-xml,C#,Asp.net,Xml,C# 3.0,Linq To 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>WIUAddre
<?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>
从上面的代码中可以看出如何使用XML文件插入迭代元素?您还没有展示您在信号数据方面得到了什么,但您应该能够在最后一行现有的
新元素之后直接执行类似操作:
signals.Select(signal => new XElement("Signal",
new XElement("SiteDeviceId", signal.SiteDeviceId),
new XElement("SiteName", signal.SiteName),
new XElement("TrackName", signal.TrackName)
))
LINQtoXML足够聪明,可以递归到可迭代的参数上。这是它与LINQ的其他部分完美集成的方式之一
编辑:根据你的评论判断,你已经有了数据,但是在数据表中。您仍然可以使用相同的方法使用<代码> DATActudi.asQueEnable()。选择(行=……)< /代码>,但我个人强烈认为首先将其转换为强类型集合,以保持代码简单和可维护。< /P> < P>您可以创建“业务对象”的中间集合。然后使用DataContractSerializer对其进行序列化。您的信号是否在某种类型的集合中?使用以下代码进行解析:for(int i=0;isignals.Select(signal => new XElement("Signal",
new XElement("SiteDeviceId", signal.SiteDeviceId),
new XElement("SiteName", signal.SiteName),
new XElement("TrackName", signal.TrackName)
))