Firdavs Kurbonov
Firdavs Kurbonov

Reputation: 1252

How to add attribute to root XML in my case?

XML is created by following code.

DataSet das = new DataSet();
das = ds.Copy();
das.DataSetName = "Stock";

das.Tables[0].TableName = "Assortment";
das.Tables[0].Columns[1].ColumnName = "Item";
das.Tables[0].Columns[2].ColumnName = "Quantity";
das.Tables[0].Columns[3].ColumnName = "Price";
das.Tables[0].Columns[4].ColumnName = "ValidDate";
das.Tables[0].Columns[5].ColumnName = "Summ";
das.Tables[0].Columns[6].ColumnName = "Manufacturer";
das.Tables[0].Columns[7].ColumnName = "Supplier";
das.WriteXml(LocalPath);

I get following xml:

<Stock>
  <Assortment>
    <ID>1</ID>
    <Item>L - тироксин   Б/Х таб  100мкг  №50</Item>
    <Quantity>12</Quantity>
    <Price>41496.0000</Price>
    <ValidDate>01.01.1999</ValidDate>
    <Summ>497952.0000</Summ>
    <Manufacturer>Заглушка</Manufacturer>
    <Supplier>Заглушка</Supplier>
  </Assortment>
  <Assortment>
    <ID>1242</ID>
    <Item>L - тироксин   Б/Х таб  100мкг  №50</Item>
    <Quantity>12</Quantity>
    <Price>10.8000</Price>
    <ValidDate>01.01.1999</ValidDate>
    <Summ>129.6000</Summ>
    <Manufacturer>Заглушка</Manufacturer>
    <Supplier>Заглушка</Supplier>
  </Assortment>
</Stock>

How to add attribute to root XML? I want like this

<Stock Date="11.11.2013">
  <Assortment>
    <ID>1</ID>
    <Item>L - тироксин   Б/Х таб  100мкг  №50</Item>
    <Quantity>12</Quantity>
    <Price>41496.0000</Price>
    <ValidDate>01.01.1999</ValidDate>
    <Summ>497952.0000</Summ>
    <Manufacturer>Заглушка</Manufacturer>
    <Supplier>Заглушка</Supplier>
  </Assortment>
  <Assortment>
    <ID>1242</ID>
    <Item>L - тироксин   Б/Х таб  100мкг  №50</Item>
    <Quantity>12</Quantity>
    <Price>10.8000</Price>
    <ValidDate>01.01.1999</ValidDate>
    <Summ>129.6000</Summ>
    <Manufacturer>Заглушка</Manufacturer>
    <Supplier>Заглушка</Supplier>
  </Assortment>

Adding date attribute to Stock. Because I want to read Date when XML was created. Thanks.

Upvotes: 3

Views: 3197

Answers (3)

KyleMit
KyleMit

Reputation: 29829

Just to flesh out Heinzi's answer a little bit more, you can do the following:

I've added this as an extension method to add a Date Attribute to the root element of any dataset:

public static void WriteXmlWithCurrentDate(this DataSet ds, string fileName)
{
    // Create the MemoryStream to write with. 
    using (MemoryStream stream = new MemoryStream())
    {
        // Write to stream with the WriteXml method.
        ds.WriteXml(stream);
        // Reset stream to origin
        stream.Seek(0, SeekOrigin.Begin);
        // Load stream as XDocument
        XDocument xdoc = XDocument.Load(stream);
        // get current date as string
        string today = DateTime.Today.ToString("d", new CultureInfo("ru-RU"));
        // Set date attribute on root element
        xdoc.Root.SetAttributeValue("Date", today);
        // Save to file as XML
        xdoc.Save(fileName);
    }
}

Then you could call it like this:

DataSet ds = new DataSet("Stock");
ds.Tables.Add(new DataTable("Assortment"));
ds.Tables[0].Columns.Add("Item", typeof(string));
ds.Tables[0].Columns.Add("Quantity", typeof(Int16));
ds.Tables[0].Rows.Add("Sock", 1);
ds.Tables[0].Rows.Add("Puppet", 2);

ds.WriteXmlWithCurrentDate(@"c:\temp\xml.xml");

Which will produce the following XML:

<?xml version="1.0" encoding="utf-8"?>
<Stock Date="13.11.2013">
  <Assortment>
    <Item>Sock</Item>
    <Quantity>1</Quantity>
  </Assortment>
  <Assortment>
    <Item>Puppet</Item>
    <Quantity>2</Quantity>
  </Assortment>
</Stock>

For completeness' sake, here are all the API's involved

Upvotes: 2

crthompson
crthompson

Reputation: 15865

If you add your attribute to the extended properties:

das.ExtendedProperties.Add("Date", "11.11.2013");

you can write it with XmlWriteMode.WriteSchema

var das = new DataSet {DataSetName = "Stock"};
das.ExtendedProperties.Add("Date", "11.11.2013");
das.WriteXml(@"c:\temp\xml.xml", XmlWriteMode.WriteSchema);

Then if you want to read the value:

var xs = XNamespace.Get("http://www.w3.org/2001/XMLSchema");
var msprop = XNamespace.Get("urn:schemas-microsoft-com:xml-msprop");
var xml = XDocument.Load(@"c:\temp\xml.xml");
var attr = xml.Descendants(xs + "element").First();
var date = attr.Attributes().First(x => x.Name == msprop + "Date").Value;

Upvotes: 5

Heinzi
Heinzi

Reputation: 172200

I don't think it is possible directly with DataSet.WriteXml. However, the following should work:

Upvotes: 1

Related Questions