1

I've been studying up and prepping serialization of class objects. The purpose of my code is to pass a json string to a WCF that is stored in the following class object:

[XmlRoot("ROWSET")]
     public class Person
    {

    [XmlElement("FIRST_NAME")]
    public string first_name { get; set; }

    [XmlElement("MIDDLE_NAME")]
    public string middle_name { get; set; }

    [XmlElement("LAST_NAME")]
    public string last_name { get; set; }

}

Simple so far. Next, i have a code in place to serialize the object into XML and strip out the headerinformation in preparation to send the XML string to be passed to Oracle as a CLOB and then stored in a table. This is the result:

<ROWSET>
  <FIRST_NAME>John</FIRST_NAME>
  <MIDDLE_NAME>James</MIDDLE_NAME>
  <LAST_NAME>Smith</LAST_NAME>
</ROWSET>

Now - that looks ready to send to Oracle to be inserted to a table. The problem is, I need to send it as Oracle XML format - known as Oracle Canonical format - which should be as:

<ROWSET>
  **<ROW>**
  <FIRST_NAME>John</FIRST_NAME>
  <MIDDLE_NAME>James</MIDDLE_NAME>
  <LAST_NAME>Smith</LAST_NAME>
  **</ROW>**
</ROWSET>"

I'm trying to understand serialize to a point where I want to insert between the column data. Has anyone some something similar?

thanks, Glenn

1 Answer 1

1

I would shape the DTO accordingly:

[XmlRoot("ROWSET")]
public class PersonRowSet
{
    [XmlElement("ROW")]
    public Person Item {get;set;}
    // ^^^ could perhaps be public List<Person> Items {get;set;}
}
public class Person
{
    [XmlElement("FIRST_NAME")]
    public string first_name { get; set; }

    [XmlElement("MIDDLE_NAME")]
    public string middle_name { get; set; }

    [XmlElement("LAST_NAME")]
    public string last_name { get; set; }
}

Also note that you shouldn't need to "strip out" the header junk - if you configure the writer correctly it won't be added in the first place:

using (var sw = new StringWriter())
{
    using (var xw = XmlWriter.Create(sw, new XmlWriterSettings {
        OmitXmlDeclaration = true }))
    {
        var obj = new PersonRowSet
        {
            Item = new Person
            {
                first_name = "John",
                last_name = "Smith",
                middle_name = "James"
            }
        };
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        var ser = new XmlSerializer(typeof(PersonRowSet));
        ser.Serialize(xw, obj, ns);

    }
    string xml = sw.ToString();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Incredible stuff - got most of it working just the way I wanted. One last question - I passed the object as person ie: public string SavePersons(Person peeps) -- I would have to assign peeps to item if so how?
@dawriter new PersonRowSet { Item = peeps }

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.