-2

I have to interact with a repository of documents, where each document is inserted with metadata contained in an XML document.
Now, at some point, this metadata must be generated from scratch. I thought that using a class to create this XML will be the best solution. I generate the class using the Developer Command Prompt, this is the result:

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

This class shoud be able to generate XML documents like this:

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

QUESTION 1: Is this class enough descriptive to generate this kind of XML documents? (Note the prefix on every tag and how every tag have a different name, also I must be able to insert any number of elements)
QUESTION 2: How I insert elements in an instance of this class? This elements have the form of a KeyValuePair, where the key represent the tag name.
QUESTION 3: How do I serialize this class into a stream and into a Base64 string from there?

Thanks in advance

Yasel
  • 2,920
  • 4
  • 40
  • 48

2 Answers2

1

Try this

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;}
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Hi @jdweng everything looks great, except for the fact that the xml elements are varied, I showed some fields as example, but the truth is that this fields are defined by the user. How could I do that? – Yasel Jun 08 '15 at 02:37
  • 1
    Can you change the format of XML? It is easy if you can use something like this . The issue is you can't create an xmlelement as stand-alone. It must be part of a document. – jdweng Jun 08 '15 at 04:11
  • Unfortunately I can change the structure. How I create the document? If this help me I'll dispose the idea of using a class... – Yasel Jun 08 '15 at 13:55
1

I would do it like this

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; } 
    }
}
​
​
jdweng
  • 33,250
  • 2
  • 15
  • 20