如何使用C#通过键值对创建和填充XML?

时间:2019-07-11 16:08:09

标签: c# xml xmlwriter

我有一个XML文件,我试图在C#中选择重新创建一个XML文件。该XML文件将为用户提供一些用户选择的信息和数据,这些信息和数据的头部包含key:value对。我应该如何生成XML?如何使用C#方法在XML上进行处理?

我尝试使用XmlWriter,但不完全了解其用法以及如何使用它格式化/写入信息。

<PULSE version="2.0">
    <TIME_STAMP value="" timezone="" />
    <CARD type="" version="" />
    <INFORMATION>
        <GENERAL key="" value="" />
    </INFORMATION>
    <DATA_PACKET time_offset="">
        <DATA key="1" value="1" />
        <DATA key="2" value="1" />
        <DATA key="3" value="0" />
    </DATA_PACKET>
    <DATA_PACKET time_offset="">
        <DATA key="1" value="1" />
        <DATA key="2" value="0" />
        <DATA key="3" value="0" />
    </DATA_PACKET>
</PULSE>`

我想创建一种示例格式的XML,以供在C#中使用。

4 个答案:

答案 0 :(得分:1)

使用xml Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication120
{
    class Program
    {
        static void Main(string[] args)
        {
            Pulse pulse = new Pulse();

            XElement xPulse = new XElement("PULSE", new object[] {
                new XAttribute("version",pulse.version),
                new XElement("TIME_STAMP", new object[] {
                    new XAttribute("value", pulse.time.ToString()),
                    new XAttribute("timezone", pulse.timezone)
                }),
                new XElement("CARD", new object[] {
                    new XAttribute("type", pulse.cardType),
                    new XAttribute("version", pulse.version)
                }),
                new XElement("INFORMATION", new object[] {
                    new XElement("GENERAL", new object[] {
                        new XAttribute("key", pulse.key),
                        new XAttribute("value", pulse.keyValue)
                    })
                }),
                pulse.data.Select(x => new XElement("DATA_PACKET", new object [] {
                    new XAttribute("time_offset", x.time.ToString()),
                    x.data.Select(y => new XElement("Data", new object[] {new XAttribute("key", y.Key), new XAttribute("value", y.Value)}))
                }))
            });

        }
    }
    public class Pulse
    {
        public DateTime time { get; set; }
        public string timezone { get; set; }
        public string cardType { get; set; }
        public string version { get; set; }
        public string key { get; set; }
        public string keyValue { get; set; }
        public List<Data> data { get;set;}
    }
    public class Data
    {
        public DateTime time { get; set; }
        public List<KeyValuePair<int, int>> data { get; set; }
    }
}

答案 1 :(得分:0)

XmlWriter的基本用法如下:

for p

这将创建这样的XML:

using (XmlWriter xmlWriter = XmlWriter.Create(...))
{
    xmlWriter.WriteStartDocument();

    xmlWriter.WriteStartElement("PULSE");
    xmlWriter.WriteAttribute("version", "2.0");

    xmlWriter.WriteStartElement("TIME_STAMP");
    xmlWriter.WriteAttribute("value", "");
    xmlWriter.WriteAttribute("timezone", "");
    xmlWriter.WriteEndElement();

    // And so on ....

    xmlWriter.WriteEndElement();

    xmlWriter.WriteEndDocument();
}

答案 2 :(得分:0)

var xml = 
    new XElement("PULSE", new XAttribute("version", "2.0"),
        new XElement("TIME_STAMP", new XAttribute("value", "val"), new XAttribute("timezone", "val")),
        new XElement("CARD", new XAttribute("type", "val"), new XAttribute("version", "val")),
        new XElement("INFORMATION",
            new XElement("GENERAL", new XAttribute("key", "val"), new XAttribute("value", "val"))),
        new XElement("DATA_PACKET", new XAttribute("time_offset", "val"),
            from x in new Dictionary<string, string> {["1"] = "1", ["2"] = "2", ["3"] = "3" }
            select new XElement("DATA", new XAttribute("key", x.Key), new XAttribute("value", x.Value))));

答案 3 :(得分:0)

尝试这样做:

XmlDocument doc = new XmlDocument();
    XmlElement root = doc.CreateElement("Country");
    XmlElement id = doc.CreateElement("Emp");
    id.SetAttribute("Name", "A");
    id.SetAttribute("Address", "Add1");
   root.AppendChild(id);
    doc.AppendChild(root);

    doc.Save("test.xml");