【问题标题】:Create xml format c# [closed]创建xml格式c#[关闭]
【发布时间】:2014-05-04 07:23:25
【问题描述】:

我想在c#中制作这样的xml格式

<?xml version='1.0' encoding='us-ascii'?> 
<root>
<key value="22.wav">
<Index>1</Index>
<Index>20</Index>
<Index>21</Index>
</key>
<key value="EFG.wav">
<Index>5</Index>
<Index>22</Index>
</key>
</root>

我是怎么形成这样的请帮帮我

【问题讨论】:

标签: c# wpf xml-parsing xml-serialization linq-to-xml


【解决方案1】:

实现此目的的最佳方法是 XMLSerialization。如下所述创建一个属性类并为其赋值:

[Serializable]
[XmlRoot("root")]
public class RootClass
{
    [XmlElement("key")]
    public List<KeyClass> key { get; set; }
}

[Serializable]
[XmlType("key")]
public class KeyClass
{
    [XmlElementAttribute("value")]
    public string KeyValue { get; set; }

    [XmlElement("Index")]
    public List<int> index { get; set; }
}

现在创建一个如下所述的 XML:

static public void SerializeXML(RootClass details)
{ 
    XmlSerializer serializer = new XmlSerializer(typeof(RootClass)); 
    using (TextWriter writer = new StreamWriter(@"C:\Xml.xml"))
    {
        serializer.Serialize(writer, details); 
    } 
}

如何使用 SerializeXML 方法赋值和生成 XML:

// Create a New Instance of the Class
var keyDetails = new RootClass();
keyDetails.key = new List<KeyClass>();

// Assign values to the Key property
keyDetails.key.Add(new KeyClass
                        {
                            KeyValue = "22.wav",
                            index = new List<int> { 1, 2, 3}
                        });

keyDetails.key.Add(new KeyClass
{
    KeyValue = "EFG.wav",
    index = new List<int> { 5 , 22 }
});

// Generate XML
SerializeXML(keyDetails);

【讨论】:

  • 如何动态添加 KeyValue 和索引。
  • 动态是什么意思?您始终可以从控件值动态添加 KetValue。
【解决方案2】:

查看这篇文章XmlDocument fluent interface

XmlOutput xo = new XmlOutput()
                .XmlDeclaration()
                .Node("root").Within()
                     .Node("key").Attribute("value", "22.wav").Within()
                         .Node("Index").InnerText("1")
                         .Node("Index").InnerText("20")
                         .Node("Index").InnerText("21").EndWithin()
                     .Node("key").Attribute("value", "EFG.wav").Within()
                         .Node("Index").InnerText("2")
                         .Node("Index").InnerText("22");

string s = xo.GetOuterXml();
//or
xo.GetXmlDocument().Save("filename.xml");

有关如何在代码中构建 xml 的其他方式,请查看以下答案 What is the best way to build XML in C# code

【讨论】:

    【解决方案3】:

    您可以使用以下代码:

        XDocument doc = new XDocument(new XDeclaration("1.0", "us-ascii", null),
            new XElement("root",
                new XElement("key", new XAttribute("value", "22.wav"),
                    new XElement("Index", 1),
                    new XElement("Index", 20),
                    new XElement("Index", 21)),
                new XElement("key", new XAttribute("value", "EFG.wav"),
                    new XElement("Index", 5),
                    new XElement("Index", 22))));
        doc.Save(fileName);
    

    【讨论】:

    • 我如何动态
    • @munisamy:使用相同的方法。首先创建静态部分(声明和根元素),然后动态创建和添加元素(使用XElement.Add方法添加子元素)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    相关资源
    最近更新 更多