Xml 序列化 - 创建对象层次结构以实现预定义的 xml 结构

发布于 2024-12-01 01:31:34 字数 2240 浏览 3 评论 0原文

我需要创建以下 XML,并希望通过 XmlSerialization 创建它,以便数据本身可以通过对象动态生成。

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

我最初想出了这个,但它并不完全有效(事实上,其中一些完全是错误的)。有什么想法吗?

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

编辑 - 太平洋标准时间 8 月 22 日晚上 11:24
当我说这完全是错误的时,我所要做的就是查看它并意识到数据结构不会映射到所需的 XML。真正让我困惑的部分是嵌套图结构。
我以前不知道如何通过 XSD 从 XML 生成类。它看起来非常有用和有帮助。
感谢所有提供解决方案的人

I need to create the following XML and want to create it via XmlSerialization so the data itself can be generated dynamically via objects.

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

I originally came up with this but it doesn't completely work (in fact, some of it is just plain wrong). Any ideas?

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

EDIT - 8/22 11:24pm PST
When I said it was just plain wrong, all I had to do was look at it and realize that the data structures wouldn't map to the desired XML. The part that was really throwing me was the nested graphs structure.
I previously did not know about generating classes from XML via the XSD. It looks very useful and helpful.
Thanks to all who provided solutions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╭ゆ眷念 2024-12-08 01:31:34

这是一个快速项目。

我使用这个类或其变体:
C# 的通用 XML 序列化器类和 XML 序列化使用示例

下面是您需要的代码。

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

以下是如何使用它。

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

Here is a quick project.

I use this class or a variant of it:
Generic XML Serializer Class for C# and an XML Serialization usage example

Then here is the code you need.

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

Here is how to use it.

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");
    }
}
笑饮青盏花 2024-12-08 01:31:34

您可以使用 XSD.exe 生成该类,(我认为)它是 Visual Studio 的一部分。

如果您打开 Visual Studio 命令提示符,则可以访问它。

  1. 在硬盘上的某个位置创建一个物理 XML 文件。 (我使用了您的 xml 并打包了 example.xml)

  2. 打开 Visual Studio 命令提示符

  3. 导航到目录

  4. 键入 ' xsdsample.xml'(不带引号)这将生成sample.xsd

  5. 类型为'xsd Sample.xsd /c'(不带引号)这将生成sample.cs

设置使用 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;
        }
    }
}

有点乱,但是节省了你很多时间和精力。

You can generate the class using XSD.exe which (i think) ships as part of visual studio.

If you open up Visual Studio Command Prompt you can access it.

  1. Create a physical XML file on your harddrive somewhere. (I used your xml and crated sample.xml)

  2. Open up Visual Studio Command Prompt

  3. Navigate to the directory

  4. type 'xsd sample.xml' (without the quotes) this will generate sample.xsd

  5. type in 'xsd sample.xsd /c' (without the quotes) this will generate sample.cs

Setting environment for using Microsoft Visual Studio 2008 x86 tools.

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:\>

Generates:

<?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>

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

It's a little bit messy but saves you a lot of time and effort.

柒七 2024-12-08 01:31:34

示例 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 BarChartSeriesEntity
{
    [XmlElement("series")]
    public BarChartSeriesValue[] SeriesValues { get; set; }
    [XmlElement("graphs")]
    public BarChartGraphsValue[] GraphsValues { get; set; }

    public class BarChartSeriesValue
    {
        [XmlElement("value")]
        public SeriesValueEntity[] Values { get; set; }
    }

    public class SeriesValueEntity
    {
        [XmlAttribute("xid")]
        public string Xid { get; set; }
        [XmlText]
        public string Value { get; set; }
    }


    public class BarChartGraphsValue
    {
        [XmlElement("graph")]
        public BarChartGraphValue[] Graphs { get; set; }
    }

    public class BarChartGraphValue
    {
        [XmlAttribute("gid")]
        public string Gid { get; set; }
        [XmlElement("value")]
        public GraphValueEntity[] Values { get; set; }
    }

    public class GraphValueEntity
    {
        [XmlAttribute("xid")]
        public string Xid { get; set; }
        [XmlAttribute("color")]
        public string Color { get; set; }
        [XmlText]
        public string Value { get; set; }
    }
}

Sample 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>

Class:

[XmlRoot("chart")]
public class BarChartSeriesEntity
{
    [XmlElement("series")]
    public BarChartSeriesValue[] SeriesValues { get; set; }
    [XmlElement("graphs")]
    public BarChartGraphsValue[] GraphsValues { get; set; }

    public class BarChartSeriesValue
    {
        [XmlElement("value")]
        public SeriesValueEntity[] Values { get; set; }
    }

    public class SeriesValueEntity
    {
        [XmlAttribute("xid")]
        public string Xid { get; set; }
        [XmlText]
        public string Value { get; set; }
    }


    public class BarChartGraphsValue
    {
        [XmlElement("graph")]
        public BarChartGraphValue[] Graphs { get; set; }
    }

    public class BarChartGraphValue
    {
        [XmlAttribute("gid")]
        public string Gid { get; set; }
        [XmlElement("value")]
        public GraphValueEntity[] Values { get; set; }
    }

    public class GraphValueEntity
    {
        [XmlAttribute("xid")]
        public string Xid { get; set; }
        [XmlAttribute("color")]
        public string Color { get; set; }
        [XmlText]
        public string Value { get; set; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文