1

I have a table that must be serialize.

its my class, i add table values to it

  public class NewCVXml
{
    [XmlArray]
    [XmlArrayItem(ElementName = "Name")]
    public List<string> FieldFirst { get; set; }
    [XmlArray]
    [XmlArrayItem(ElementName = "Value")]
    public List<string> FieldSecond { get; set; }
    [XmlArray]
    [XmlArrayItem(ElementName = "State")]
    public List<string> FieldThird { get; set; }

}

this is value came from ajax (Request.Form)

{field1=name1&field1=name2&field1=name3&field2=value1&field2=value2&field2=value3&field3=Private&field3=Public&field3=Public}

and this is my c# function that called by ajax (for posting)

       string xmlText;
        NewCVXml newCv = new NewCVXml();
        newCv.FieldFirst =  Request.Form.GetValues(0).ToList();
        newCv.FieldSecond = Request.Form.GetValues(1).ToList();
        newCv.FieldThird = Request.Form.GetValues(2).ToList();

        try
        {
            XmlSerializer serializer = new XmlSerializer(newCv.GetType());
            MemoryStream ms = new MemoryStream();
            using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8) { Formatting = Formatting.Indented })
            {

                serializer.Serialize(writer, newCv);
                ms = (MemoryStream)writer.BaseStream;
                xmlText = new UTF8Encoding().GetString(ms.ToArray());
                ms.Dispose();
                return Content(xmlText);
            }
        }
        catch (Exception e)
        {

            return Content( "Hata: " + e.Message);

        }

and this xml creates

    <?xml version="1.0" encoding="utf-8"?>
<NewCVXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FieldFirst>
    <Name>name1</Name>
    <Name>name2</Name>
    <Name>name3</Name>
  </FieldFirst>
  <FieldSecond>
    <Value>value1</Value>
    <Value>value2</Value>
    <Value>value3</Value>
  </FieldSecond>
  <FieldThird>
    <State>Private</State>
    <State>Public</State>
    <State>Public</State>
  </FieldThird>
</NewCVXml>

my table

But I want something like that xml

<?xml version="1.0" encoding="utf-8"?>
<NewCVXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Rows>
    <name1 state="Private">value1</name1>
    <name2 state="Public">value2</name2>
    <name3 state="Public">value3</name3>
</Rows>
</NewCVXml>

I dont know how I can create like that xml. I cannot use a class for each row. Because cells of rows are changeable. I mean, it can be name1 or XXX or YYY or ... it depens on user. Everything is dynamic then I think that I need to use a string list and create tagname from list values.

2 Answers 2

1

You class structure does not serialize to XML in the way you would like using the standard XmlSerializer. Instead, you could implement custom serialization using IXmlSerializable.

http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

public class NewCVXml : IXmlSerializable {

    public List<string> FieldFirst { get; set; }
    public List<string> FieldSecond { get; set; }
    public List<string> FieldThird { get; set; }

    public void WriteXml (XmlWriter writer)
    {
        // Custom Serialization Here
    }

    public void ReadXml (XmlReader reader)
    {
        // Custom Deserialization Here
    }

    public XmlSchema GetSchema()
    {
        return(null);
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. I think that is what I really need. Thank you , I will search about custom xml serialization :)
0

as Robert Graves said, I needed a custom xml serializer. I used code which Robert gave and edit it. It works now, maybe some people will need it then I put it here

using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace E_Cv.Functions
{
    public class NewCVXml :IXmlSerializable
    {

        public List<string> FieldFirst { get; set; }
        public List<string> FieldSecond { get; set; }
        public List<string> FieldThird { get; set; }

        public void WriteXml(XmlWriter writer)
        {


            for (int i = 0; i < FieldFirst.Count; i++)
            {

                writer.WriteStartElement("Satir");

                writer.WriteAttributeString("AlanIsmi",FieldFirst[i]);
                //writer.WriteString(FieldFirst[i]);    
                writer.WriteAttributeString("AlanTuru", FieldThird[i]);
                writer.WriteString(FieldSecond[i]); 
                writer.WriteEndElement();
            }


        }

        public void ReadXml(XmlReader reader)
        {
            // Custom Deserialization Here
        }

        public XmlSchema GetSchema()
        {
            return (null);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.