views:

376

answers:

2

Hope I chose the correct forum.

I have a dataset object with one table that comes to me from a common custom component's GetDS method. I need to pass XML to another process (chunked as byte array). I have it all working but the XML is missing some attributes that the consuming process expects.

I create a dataset object and can control the name of the TableName (root element) and the row like this:

da.Fill(ds, "Foo")
ds.DataSetName = "FooUpload"

I use the GetXML method to serialize to XML that looks like the following:

<?xml version="1.0" standalone="yes" ?> 
<FooUpload>
  <Foo>
  <FooMasterID>483</FooMasterID> 
  <Country>27</Country> 
  <PaymentCode>ANN</PaymentCode> 
  <Amount>132</Amount> 
  <PaidDate>2012-12-31 00:00:00</PaidDate> 
  <PaidBy>FooServices</PaidBy> 
  </Foo>
</FooUpload>

The calling process expects

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<FooUpload **ClientCode="FOOO" RecordCount="1" CreateDate="2008-12-09T15:02:18.920" CreateUser="valli"**>
  <Foo>
  <FooMasterID>483</FooMasterID> 
  <Country>27</Country> 
  <PaymentCode>ANN</PaymentCode> 
  <Amount>132</Amount> 
  <PaidDate>2012-12-31 00:00:00</PaidDate> 
  <PaidBy>FooServices</PaidBy> 
  </Foo>
</FooUpload>

Note the attributes on the FooUpload element. This node is the name of the DataTable in the DataSet.

I have searched for how to control the XMLSerializer and find lots of examples for custom objects. I even found examples of setting the column mapping to be MappingType.Attribute which is close but I need to do this with the root element which is actually the TableName for the dataset.

I feel that I am close and if I do not find a more elegant solution I will have to create a hack like looping and spitting out changed string plus rest of the XML.

Thanks in advance (fingers crossed)!

A: 

You can feed the output of GetXML into a XmlDocument and add the attributes afterwards. For example:

 XmlDocument xdoc = new XmlDocument();
 xdoc.LoadXml(ds.GetXml());
 XmlAttribute attr=xdoc.CreateAttribute("ClientCode");
 attr.Value = "FOOOO";
 xdoc.DocumentElement.Attributes.Append(attr);

Then you can save the xdoc into a file, or put it into a string, for example:

 XmlTextWriter xw = new XmlTextWriter(new MemoryStream(),Encoding.UTF8);
 xdoc.Save(xw);
 xw.BaseStream.Position = 0;
 StreamReader sr = new StreamReader(xw.BaseStream);
 string result = sr.ReadToEnd();
Doc Brown
I think this may indeed be the only way. I found some indications that it is "by design" that DataSet serialization does not handle mixed types.
sdmcnitt
@sdmcnitt: then please vote up my answer. And if you think you will use it, accept it.
Doc Brown
I need a "15 reputation" to vote up it says. I guess I am infamous.
sdmcnitt
A: 

This looks pretty much as what you are looking for.

uli78
Thanks uli78. It looks like the ExtendedProperties would indeed serialize into attributes but it looks like it would only be applicable to the table columns -- not to the table itself.Note the attributes on the FooUpload element. This node is the name of the DataTable in the DataSet. It seems that when the DataSet serializes, the root element is the table name.Is there such a thing as attributes for the table in the DataSet?
sdmcnitt
Yes, there is. "<DataSet>.ExtendedProperties"
uli78