如何通过 class 操作未创建的 XML 文档

How to manipulate an uncreated XML document through a class

我必须与文档存储库进行交互,其中每个文档都插入了 XML 文档中包含的元数据。
现在,在某些时候,这个元数据必须从头开始生成。我认为使用 class 来创建这个 XML 将是最好的解决方案。 我使用开发人员命令提示符生成 class,这是结果:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://example.com/metadata")]
[System.Xml.Serialization.XmlRootAttribute(ElementName = "Metadata", Namespace="http://example.com/metadata", IsNullable=false)]
public partial class XmlMetadata {

    private System.Xml.XmlElement[] anyField;

    [System.Xml.Serialization.XmlAnyElementAttribute()]
    public System.Xml.XmlElement[] Any {
        get {
            return this.anyField;
        }
        set {
            this.anyField = value;
        }
    }
}

这个 class 应该能够生成 XML 文档,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<md:Metadata xmlns:md="http://example.com/metadata">
   <md:Cert>0001</md:Cert>
   <md:Model>Test</md:Model>
   <md:Created>2015-05-21</md:Created>
</md:Metadata>

问题 1:这个 class 的描述是否足以生成这种 XML 文档? (注意每个标签的前缀以及每个标签如何有不同的名称,而且我必须能够插入任意数量的元素)
问题 2: 如何在此 class 的实例中插入元素?此元素具有 KeyValuePair 的形式,其中键代表标签名称。
问题 3: 如何将此 class 序列化为流并从那里序列化为 Base64 字符串?

提前致谢

试试这个

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


namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlMetadata data = new XmlMetadata()
            {
                cert = "0001",
                model = "Test",
                created = DateTime.Parse("2015-05-21")
            };


            XmlSerializer serializer = new XmlSerializer(typeof(XmlMetadata));
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("md", "http://example.com/metadata");

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, data, ns);
            writer.Flush();
            writer.Close();
            writer.Dispose();


            XmlSerializer xs = new XmlSerializer(typeof(XmlMetadata));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            XmlMetadata newData = (XmlMetadata)xs.Deserialize(reader);
        }
    }
    [XmlRootAttribute(ElementName = "Metadata", Namespace = "http://example.com/metadata", IsNullable = false)]
    public class XmlMetadata
    {

       [XmlElement(ElementName = "Cert", Namespace = "http://example.com/metadata", IsNullable = false)]
       public string cert {get;set;}
       [XmlElement(ElementName = "Model", Namespace = "http://example.com/metadata", IsNullable = false)]
       public string model {get;set;}
       [XmlElement(ElementName = "Created", Namespace = "http://example.com/metadata", IsNullable = false)]
       public DateTime created {get;set;}
    }
}
​

我会这样做

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


namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlMetadata data = new XmlMetadata(){
                elemements = new List<Element>(){
                    new Element(){ name = "cert", value = "0001"},
                    new Element(){ name = "model", value = "Test"},
                    new Element(){ name = "created", value = "2015-05-21"}
                }    
            };


            XmlSerializer serializer = new XmlSerializer(typeof(XmlMetadata));
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("md", "http://example.com/metadata");

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, data, ns);
            writer.Flush();
            writer.Close();
            writer.Dispose();


            XmlSerializer xs = new XmlSerializer(typeof(XmlMetadata));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            XmlMetadata newData = (XmlMetadata)xs.Deserialize(reader);
        }
    }
    [XmlRootAttribute(ElementName = "Metadata", Namespace = "http://example.com/metadata", IsNullable = false)]
    public class XmlMetadata
    {
        [XmlElement(ElementName = "Element", Namespace = "http://example.com/metadata", IsNullable = false)]
        public List<Element> elemements { get; set; }
    }
    [XmlRootAttribute(ElementName = "Element", Namespace = "http://example.com/metadata", IsNullable = false)]
    public class Element
    {
        [XmlAttribute(AttributeName = "name",Namespace = "http://example.com/metadata")]
        public string name {get; set; }
        [XmlAttribute(AttributeName = "value", Namespace = "http://example.com/metadata")]
        public string value { get; set; } 
    }
}
​
​