简体   繁体   中英

How to insert iteration Element in XMl file using C#.net?

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM