Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类的xml输出_C#_Xml_Class - Fatal编程技术网

C# 类的xml输出

C# 类的xml输出,c#,xml,class,C#,Xml,Class,我正在尝试生成如下所示的xml输出(基于类): <cdsXMLEnvelope> <TestValue1>x1</TestValue1> <inner> <eTestValue1>e1</eTestValue1> </inner> <inner> <eTestValue1>e1</eTestValue1> </inner> <

我正在尝试生成如下所示的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; }
}

如果您有该模式,您可以尝试使用来为您生成C类。

我认为这是您想要的,但是您的示例并不完全一致:

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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Xml;
使用System.Xml.Serialization;
命名空间序列化
{
班级计划
{
静态void Main(字符串[]参数)
{
var信封=新的CdsXmlEnvelope
{
TestValue1=“x1”,
InnerList=新列表
{
新的CdsXmlInfo{TestValue1=“e1”},
新的CdsXmlInfo{TestValue1=“e1”},
新的CdsXmlInfo{TestValue1=“e1”},
}
};
var ns=新的XmlSerializerNamespaces();
添加(string.Empty,string.Empty);
var serializer=newxmlserializer(typeof(CdsXmlEnvelope));
使用(var stream=new MemoryStream())
{
var设置=新的XmlWriterSettings();
settings.OmitXmlDeclaration=true;
settings.Indent=true;
var writer=XmlWriter.Create(流、设置);
serializer.Serialize(writer,信封,ns);
流位置=0;
Console.WriteLine(新的StreamReader(stream.ReadToEnd());
}
Console.ReadLine();
}
}
[XmlRoot(“cdsXMLEnvelope”)]
公共类CDSXMLEVELOPE
{
[XmlElement(“TestValue1”)]
公共字符串TestValue1{get;set;}
[XmlElement(“内部”)]
公共列表InnerList{get;set;}
}
公共类CdsXmlInfo
{
[XmlElement(“TestValue1”)]
公共字符串TestValue1{get;set;}
[XmlAttribute]
公共字符串TestValue3{get;set;}
}
}
给出输出:

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

x1
e1
e1
e1

我已经这样做了,但是kruft太多了,更重要的是,我需要帮助访问实际的类(一旦它正确)——我想保持简单我知道这听起来很奇怪,但实际上我在使用自动属性和XML序列化时遇到了问题。尝试将它们切换到正常属性。这可能需要和构造器一起解决,但我还没有深入研究来解决这个问题。好吧。。。但我在子元素方面确实遇到了麻烦——在引入“内部”项之前,我确实很好地导出了xml。这看起来很完美,正是我想要的。。。我只是没能到达那里!!
<cdsXMLEnvelope>
  <TestValue1>x1</TestValue1>
  <inner>
    <TestValue1>e1</TestValue1>
  </inner>
  <inner>
    <TestValue1>e1</TestValue1>
  </inner>
  <inner>
    <TestValue1>e1</TestValue1>
  </inner>
</cdsXMLEnvelope>