Xml output from class

I am trying to generate xml output (class based) that looks like this:

<cdsXMLEnvelope>
<TestValue1>x1</TestValue1>
  <inner>
    <eTestValue1>e1</eTestValue1>
  </inner>
  <inner>
    <eTestValue1>e1</eTestValue1>
  </inner>
  <inner>
    <eTestValue1>e1</eTestValue1>
  </inner>
</cdsXMLEnvelope>

[System.Xml.Serialization.XmlRootAttribute("cdsXMLEnvelope", Namespace = "",       IsNullable = false)]
[XmlTypeAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance")]

//public class cdsXMLEnvelope : cdsXMLinfo
public class cdsXMLEnvelope
{
    [XmlElementAttribute(ElementName = "eTestValue1")]
    public string eTestValue1 { get; set; }

    [System.Xml.Serialization.XmlArrayItem("inner")]
    public cdsXMLinfo[] cdsXMLinfo { get; set; }

}

//[XmlTypeAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public class cdsXMLinfo
{
    [XmlElementAttribute(ElementName = "TestValue1")]
    public string TestValue1 { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string TestValue3 { get; set; }
}

      

please help me to intercept this code, change the class, etc. I keep trying in different ways and I get object not found and error errors when I try to serialize it to xml

also is it possible to avoid using an array for the inner elements? use lists?

I will try to perfect this question, when we go, I know people hate these long ones?

Thanks!!

         {

                cdsXMLEnvelope x = new cdsXMLEnvelope();

                x.eTestValue1 = "x1"; 

                x.cdsXMLinfo = new cdsXMLinfo[1];
                x.cdsXMLinfo[0].TestValue1 = "xi1";
                //x.cdsXMLinner[0].TestValue1 = "xi2";             

                XmlSerializer writer = new XmlSerializer(x.GetType());
                StreamWriter file = new StreamWriter("x.xml");

                writer.Serialize(file, x);
                file.Close();

        }

      

0


a source to share


2 answers


I think this gives what you want, however your example was not entirely consistent:

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

namespace Serialize
{
    class Program
    {
        static void Main( string[] args )
        {
            var envelope = new CdsXmlEnvelope
            {
                TestValue1 = "x1",
                InnerList = new List<CdsXmlInfo>
                {
                    new CdsXmlInfo {TestValue1 = "e1"},
                    new CdsXmlInfo {TestValue1 = "e1"},
                    new CdsXmlInfo {TestValue1 = "e1"},
                }
            };

            var ns = new XmlSerializerNamespaces();
            ns.Add( string.Empty, string.Empty );

            var serializer = new XmlSerializer( typeof( CdsXmlEnvelope ) );
            using( var stream = new MemoryStream() )
            {
                var settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.Indent = true;

                var writer = XmlWriter.Create( stream, settings );
                serializer.Serialize( writer, envelope, ns );
                stream.Position = 0;
                Console.WriteLine( new StreamReader( stream ).ReadToEnd() );
            }
            Console.ReadLine();
        }
    }

    [XmlRoot( "cdsXMLEnvelope" )]
    public class CdsXmlEnvelope
    {
        [XmlElement( "TestValue1" )]
        public string TestValue1 { get; set; }

        [XmlElement( "inner" )]
        public List<CdsXmlInfo> InnerList { get; set; }
    }

    public class CdsXmlInfo
    {
        [XmlElement( "TestValue1" )]
        public string TestValue1 { get; set; }

        [XmlAttribute]
        public string TestValue3 { get; set; }
    }
}

      



output:

<cdsXMLEnvelope>
  <TestValue1>x1</TestValue1>
  <inner>
    <TestValue1>e1</TestValue1>
  </inner>
  <inner>
    <TestValue1>e1</TestValue1>
  </inner>
  <inner>
    <TestValue1>e1</TestValue1>
  </inner>
</cdsXMLEnvelope>

      

+1


a source


Once you have the schema, you can try using Xsd.exe to generate C # classes for you.



0


a source







All Articles