类的xml输出

时间:2009-05-14 20:25:21

标签: c# xml class

我正在尝试生成xml输出(基于类),如下所示:

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

请帮我勾解这个代码,更改课程等等 - 我一直在尝试各种各样的方式&amp;我找不到对象&amp;尝试将其序列化为xml时出现misc错误

我也可以避免使用数组作为内部元素吗?使用列表?

我会尽力改进这个问题,我知道人们讨厌这些长期的问题

谢谢!

         {

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

        }

2 个答案:

答案 0 :(得分:1)

我认为这会给你想要的东西,但是你的例子不太一致:

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

给出输出:

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

答案 1 :(得分:0)

如果您有架构,可以尝试使用Xsd.exe为您生成C#类。