简体   繁体   English

在我的案例中如何向root XML添加属性?

[英]How to add attribute to root XML in my case?

XML is created by following code. XML由以下代码创建。

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: 我得到以下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? 如何向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. 将日期属性添加到Stock。 Because I want to read Date when XML was created. 因为我想在创建XML时读取日期。 Thanks. 谢谢。

If you add your attribute to the extended properties: 如果将属性添加到扩展属性:

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

you can write it with XmlWriteMode.WriteSchema 你可以用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;

Just to flesh out Heinzi 's answer a little bit more, you can do the following: 只是为了Heinzi的答案,你可以做到以下几点:

I've added this as an extension method to add a Date Attribute to the root element of any dataset: 我已将此添加为扩展方法,以将Date属性添加到任何数据集的根元素:

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

<?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 为了完整起见,以下是所有涉及的API

I don't think it is possible directly with DataSet.WriteXml . 我认为不可能直接使用DataSet.WriteXml However, the following should work: 但是,以下应该有效:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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