Jeyavel
Jeyavel

Reputation: 3030

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?

Upvotes: 0

Views: 849

Answers (2)

Pierreten
Pierreten

Reputation: 10147

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502076

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.

Upvotes: 1

Related Questions