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

本文关键字:XML 文档 创建 何通过 操作 | 更新日期: 2023-09-27 17:53:58

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

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

这个类应该能够生成这样的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:这个类是否足够描述生成这种XML文档?(注意每个标签上的前缀以及每个标签如何有不同的名称,我还必须能够插入任意数量的元素)
问题2:如何在这个类的实例中插入元素?这些元素具有KeyValuePair的形式,其中的键表示标记名称。
问题3:如何将该类序列化为流并从那里序列化为Base64字符串?

Thanks in advance

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

我想这样做

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

试试这个

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