Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:32:45+00:00 2026-05-24T23:32:45+00:00

I need to create the following XML and want to create it via XmlSerialization

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:32:46+00:00Added an answer on May 24, 2026 at 11:32 pm

    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");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have the following interfaces that I need to create. Can you just
how we can create new xml file by C# in the following situation: its
I need to create an installer program that will do install the following: ASP.Net
Imagine having the following scenario: You need to create a system where the Back
Oh great Stackoverflow, I beseech thee... I need to do the following: CREATE UNIQUE
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I want to do following: Make Oracle stored procedure for read all data from
I'm using xslt to create onscreen pdf's from xml and I need to hide
Now I'm working on a web project. I need to create a XML document
I figured out how to create a DOM object for XML with the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.