C# Xml序列化-创建对象层次结构以实现预定义的Xml结构
我需要创建以下XML,并希望通过XmlSerialization创建它,以便数据本身可以通过对象动态生成C# Xml序列化-创建对象层次结构以实现预定义的Xml结构,c#,xml,xml-serialization,amcharts,C#,Xml,Xml Serialization,Amcharts,我需要创建以下XML,并希望通过XmlSerialization创建它,以便数据本身可以通过对象动态生成 <chart> <series> <value xid="0">Not Submitted: 20</value> <value xid="1">Submitted: 11</value> <value xid="2">Rejected: 2</value> <
<chart>
<series>
<value xid="0">Not Submitted: 20</value>
<value xid="1">Submitted: 11</value>
<value xid="2">Rejected: 2</value>
</series>
<graphs>
<graph gid="0">
<value xid="0" color="#FF0000">20</value>
<value xid="1" color="#00FF00">11</value>
<value xid="2" color="#0000FF">2</value>
</graph>
<graph gid="1">
<value xid="0" color="#FF0000">24</value>
<value xid="1" color="#00FF00">7</value>
<value xid="2" color="#0000FF">4</value>
</graph>
</graphs>
</chart>
未提交:20
提交日期:11
拒绝:2
20
11
2.
24
7.
4.
我最初提出了这一点,但它并不完全有效(事实上,其中一些是完全错误的)。有什么想法吗
// the //chart/series/value elements with attributes
internal class BarChartSeriesValue
{
[XmlAttribute(AttributeName = "xid")]
public int Xid { get; set; }
[XmlText]
public int Value { get; set; }
}
// the //chart/graphs/graph/value elements with attributes
internal class BarChartGraphValue
{
[XmlAttribute(AttributeName = "xid")]
public int Xid { get; set; }
[XmlAttribute(AttributeName = "color")]
public string Color { get; set; }
[XmlText]
public int Value { get; set; }
}
// the //chart/series collection of values
internal class BarChartSeries : List<BarChartSeriesValue>
{
[XmlElement(ElementName = "series")]
public List<BarChartSeriesValue> Series { get; set; }
}
// the //chart/graphs/graph collection of values
internal class BarChartGraph : List<BarChartGraphValue>
{
[XmlAttribute(AttributeName = "gid")]
public int GraphId { get; set; }
}
// the //chart/graphs collection of graph elements
internal class BarChartData
{
[XmlElement(ElementName = "series")]
public BarChartSeries Series { get; set; }
[XmlElement(ElementName = "graphs")]
public BarChartGraph Graphs { get; set; }
}
//带有属性的//图表/系列/值元素
内部类BarChartSeriesValue
{
[xmldattribute(AttributeName=“xid”)]
公共int Xid{get;set;}
[XmlText]
公共int值{get;set;}
}
////chart/graphs/graph/value属性元素
内部类BarChartGraphValue
{
[xmldattribute(AttributeName=“xid”)]
公共int Xid{get;set;}
[XmlAttribute(AttributeName=“color”)]
公共字符串颜色{get;set;}
[XmlText]
公共int值{get;set;}
}
////图表/系列值集合
内部类BarChartSeries:列表
{
[xmlement(ElementName=“series”)]
公共列表系列{get;set;}
}
////chart/graph/graph值集合
内部类条形图:列表
{
[XmlAttribute(AttributeName=“gid”)]
公共int图形{get;set;}
}
////chart/graphs图形元素的集合
内部类BarChartData
{
[xmlement(ElementName=“series”)]
公共条形图系列{get;set;}
[xmlement(ElementName=“graphs”)]
公共条形图图图{get;set;}
}
编辑-太平洋标准时间8月22日11:24
当我说这是完全错误的时候,我所要做的就是查看它并意识到数据结构不会映射到所需的XML。真正让我头疼的是嵌套图结构。我以前不知道如何通过XSD从XML生成类。它看起来非常有用和有用。
感谢所有提供解决方案的人示例Xml:
<chart>
<series>
<value xid="0">0</value>
<value xid="0">0</value>
<value xid="0">0</value>
</series>
<graphs>
<graph gid="1">
<value xid="0" color="0">0</value>
<value xid="0" color="0">0</value>
<value xid="0" color="0">0</value>
</graph>
<graph gid="1">
<value xid="0" color="0">0</value>
<value xid="0" color="0">0</value>
<value xid="0" color="0">0</value>
</graph>
<graph gid="1">
<value xid="0" color="0">0</value>
<value xid="0" color="0">0</value>
<value xid="0" color="0">0</value>
</graph>
</graphs>
</chart>
这里有一个快速的项目 我使用此类或其变体: 这是你需要的代码
[XmlRoot("chart")]
public class Chart
{
[XmlElement("series")]
public Series Series { get; set; }
[XmlArray("graphs")]
[XmlArrayItem("graph")]
public Graphs Graphs { get; set; }
}
public class Series
{
[XmlElement("value")]
public List<SeriesValue> Values { get; set; }
}
public class Graphs : List<Graph>
{
}
public class Graph
{
[XmlAttribute("gid")]
public int Gid { get; set; }
[XmlElement("value")]
public List<GraphValue> Values { get; set; }
}
public class GraphValue
{
[XmlAttribute("xid")]
public int Xid { get; set; }
[XmlAttribute("color")]
public String Color { get; set; }
[XmlText]
public int Value { get; set; }
}
public class SeriesValue
{
[XmlAttribute("xid")]
public int Xid { get; set; }
[XmlText]
public String Text { get; set; }
}
[XmlRoot(“图表”)]
公共课程表
{
[XmlElement(“系列”)]
公共系列{get;set;}
[XmlArray(“图形”)]
[XmlArrayItem(“图形”)]
公共图{get;set;}
}
公开课系列
{
[XmlElement(“值”)]
公共列表值{get;set;}
}
公共类图:列表
{
}
公共类图
{
[XmlAttribute(“gid”)]
公共int Gid{get;set;}
[XmlElement(“值”)]
公共列表值{get;set;}
}
公共类图形值
{
[xmldattribute(“xid”)]
公共int Xid{get;set;}
[XmlAttribute(“颜色”)]
公共字符串颜色{get;set;}
[XmlText]
公共int值{get;set;}
}
公共类序列值
{
[xmldattribute(“xid”)]
公共int Xid{get;set;}
[XmlText]
公共字符串文本{get;set;}
}
下面是如何使用它
class Program
{
static void Main(string[] args)
{
Chart c = new Chart();
c.Series = new Series();
c.Series.Values = new List<SeriesValue>();
c.Series.Values.Add(new SeriesValue() { Xid = 0, Text = "Not Submitted: 20" });
c.Series.Values.Add(new SeriesValue() { Xid = 1, Text = "Submitted: 11" });
c.Series.Values.Add(new SeriesValue() { Xid = 2, Text = "Rejected: 2" });
c.Graphs = new Graphs();
c.Graphs.Add(new Graph() { Gid = 0 });
c.Graphs[0].Values = new List<GraphValue>();
c.Graphs[0].Values.Add(new GraphValue() { Xid = 0, Color = "#FF0000", Value = 20 });
c.Graphs[0].Values.Add(new GraphValue() { Xid = 1, Color = "#00FF00", Value = 11 });
c.Graphs[0].Values.Add(new GraphValue() { Xid = 2, Color = "#0000FF", Value = 2 });
c.Graphs.Add(new Graph() { Gid = 1 });
c.Graphs[1].Values = new List<GraphValue>();
c.Graphs[1].Values.Add(new GraphValue() { Xid = 0, Color = "#FF0000", Value = 24 });
c.Graphs[1].Values.Add(new GraphValue() { Xid = 1, Color = "#00FF00", Value = 7 });
c.Graphs[1].Values.Add(new GraphValue() { Xid = 2, Color = "#0000FF", Value = 4 });
// Make sure it is Serializable
Serializer.SerializeToXML<Chart>(c, "chart.xml");
// Make sure it is Deserializable
Chart c2 = Serializer.DeserializeFromXML<Chart>("chart.xml");
}
}
类程序
{
静态void Main(字符串[]参数)
{
图表c=新图表();
c、 系列=新系列();
c、 Series.Values=新列表();
c、 Series.Values.Add(新的SeriesValue(){Xid=0,Text=“未提交:20”});
c、 Add(新的SeriesValue(){Xid=1,Text=“Submitted:11”});
c、 Add(新的SeriesValue(){Xid=2,Text=“拒绝:2”});
c、 图=新图();
c、 Add(newgraph(){Gid=0});
c、 图[0]。值=新列表();
c、 图形[0].Values.Add(新图形值(){Xid=0,Color=“#FF0000”,Value=20});
c、 图[0].Values.Add(新的图值(){Xid=1,Color=“#00FF00”,Value=11});
c、 图[0].Values.Add(新的图值(){Xid=2,Color=“#0000FF”,Value=2});
c、 Add(newgraph(){Gid=1});
c、 图[1]。值=新列表();
c、 图[1].Values.Add(新的GraphValue(){Xid=0,Color=“#FF0000”,Value=24});
c、 图[1].Values.Add(新的GraphValue(){Xid=1,Color=“#00FF00”,Value=7});
c、 图[1].Values.Add(新的图值(){Xid=2,Color=“#0000FF”,Value=4});
//确保它是可序列化的
SerializeToXML(c,“chart.xml”);
//确保它是反序列化的
Chart c2=序列化程序.DeserializeFromXML(“Chart.xml”);
}
}
您可以使用XSD.exe生成类,我认为它是visual studio的一部分
如果打开Visual Studio命令提示符,则可以访问它
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.5446
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)]
public partial class value {
private string xidField;
private string colorField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string xid {
get {
return this.xidField;
}
set {
this.xidField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string color {
get {
return this.colorField;
}
set {
this.colorField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class chart {
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("graphs", typeof(chartGraphs), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("series", typeof(value[]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("value", typeof(value), IsNullable=true)]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class chartGraphs {
private chartGraphsGraph[] graphField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("graph", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public chartGraphsGraph[] graph {
get {
return this.graphField;
}
set {
this.graphField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class chartGraphsGraph {
private value[] valueField;
private string gidField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("value", IsNullable=true)]
public value[] value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string gid {
get {
return this.gidField;
}
set {
this.gidField = value;
}
}
}
设置使用Microsoft Visual Studio 2008 x86工具的环境
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC>G:
G:\>xsd sample.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'G:\sample.xsd'.
G:\>xsd sample.xsd /c
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'G:\sample.cs'.
G:\>
生成:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="chart" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="value" nillable="true">
<xs:complexType>
<xs:simpleContent msdata:ColumnName="value_Text" msdata:Ordinal="1">
<xs:extension base="xs:string">
<xs:attribute name="xid" type="xs:string" />
<xs:attribute name="color" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="chart" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="value" />
<xs:element name="series">
<xs:complexType>
<xs:sequence>
<xs:element ref="value" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="graphs">
<xs:complexType>
<xs:sequence>
<xs:element name="graph" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="value" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="gid" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
和sample.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.5446
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)]
public partial class value {
private string xidField;
private string colorField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string xid {
get {
return this.xidField;
}
set {
this.xidField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string color {
get {
return this.colorField;
}
set {
this.colorField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class chart {
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("graphs", typeof(chartGraphs), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("series", typeof(value[]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("value", typeof(value), IsNullable=true)]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class chartGraphs {
private chartGraphsGraph[] graphField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("graph", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public chartGraphsGraph[] graph {
get {
return this.graphField;
}
set {
this.graphField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class chartGraphsGraph {
private value[] valueField;
private string gidField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("value", IsNullable=true)]
public value[] value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string gid {
get {
return this.gidField;
}
set {
this.gidField = value;
}
}
}
//------------------------------------------------------------------------------
//
//这段代码是由一个工具生成的。
//运行时版本:2.0.50727.5446
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
//
//------------------------------------------------------------------------------
使用System.Xml.Serialization;
//
//此源代码由xsd自动生成,版本=2.0.50727.3038。
//
///
[系统代码]