如何使用 C#.net 在 XMl 文件中插入迭代元素?

发布于 2024-10-03 11:29:05 字数 2157 浏览 4 评论 0原文

我想根据我的要求插入迭代元素(信号),如下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凤舞天涯 2024-10-10 11:29:05

您还没有显示信号数据方面的内容,但是您应该能够在最终现有的 new XElement 行之后直接执行类似的操作:

signals.Select(signal => new XElement("Signal",
    new XElement("SiteDeviceId", signal.SiteDeviceId),
    new XElement("SiteName", signal.SiteName),
    new XElement("TrackName", signal.TrackName)
))

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:

signals.Select(signal => new XElement("Signal",
    new XElement("SiteDeviceId", signal.SiteDeviceId),
    new XElement("SiteName", signal.SiteName),
    new XElement("TrackName", signal.TrackName)
))

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.

茶花眉 2024-10-10 11:29:05

您可以从中创建“业务对象”的中间集合,然后仅使用 DataContractSerializer 对其进行序列化。

You can create an intermediate collection of "Business objects" out of it and then just use the DataContractSerializer to serialize it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文