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

时间:2013-11-12 16:29:17

标签: c# xml

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);

我得到以下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>

如何向root XML添加属性?我想要这样

<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>

将日期属性添加到Stock。 因为我想在创建XML时读取日期。 感谢。

3 个答案:

答案 0 :(得分:5)

如果将属性添加到扩展属性:

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

您可以使用XmlWriteMode.WriteSchema

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

然后,如果你想读取值:

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;

答案 1 :(得分:2)

只是为了充实Heinzi的答案,你可以做到以下几点:

我已将此作为扩展方法添加到任何数据集的根元素中添加日期属性:

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);
    }
}

然后你可以这样称呼它:

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");

这将生成以下 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>

为了完整起见,以下是所有涉及的API

答案 2 :(得分:1)

我认为不可能直接使用DataSet.WriteXml。但是,以下内容应该有效: