tags:

views:

106

answers:

2

Hello

I'm trying to generate xml output (based on a class) 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 hook this code up, change the class etc-- I keep trying a variety of ways & I get object not found & misc errors when trying to serialize it to xml

also can I avoid using an array for the inner elements? do I use lists?

I'll try to refine this question as we go, I know people hate these long ?'s

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();

        }
A: 

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

Erich Mirabal
I've done this, but there is too much kruft, Also more importantly I need help accessing the actual class (once its correct)-- I want to keep this bare bones simple
Scott Kramer
I know this sounds weird, but I've actually had problems using automatic properties and XML serialization. Try switching them to normal properties. This might have to with the constructors, but I have not dug deeper to figure it out, yet.
Erich Mirabal
ok... but I really am having trouble with the sub elements-- I do get xml out just fine before I introduce the "inner" items
Scott Kramer
+1  A: 

I think this gives what you want, however your example was not quite 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; }
    }
}

giving output:

<cdsXMLEnvelope>
  <TestValue1>x1</TestValue1>
  <inner>
    <TestValue1>e1</TestValue1>
  </inner>
  <inner>
    <TestValue1>e1</TestValue1>
  </inner>
  <inner>
    <TestValue1>e1</TestValue1>
  </inner>
</cdsXMLEnvelope>
Greg Bacchus
this looks perfect, exactly what I was looking for... I just couldn't quite get there!!
Scott Kramer