-
May 8th, 2008, 04:25 AM
#1
XML message generation
Hello everyone,
I need to generate some XML message, but the message can not be reflected from members or properties of class instance directly.
Currently, I am using StringBuilder to append strings into XML message manually (e.g. to generate the element's hierarchies and add attributes to element), I think this method is stupid.
Any better approaches recommended?
thanks in advance,
George
-
May 8th, 2008, 05:58 AM
#2
Re: XML message generation
I need to generate some XML message, but the message can not be reflected from members or properties of class instance directly.
I don't understand what you are trying to say. Can you explain it again? (or give an example?)
-
May 8th, 2008, 08:11 AM
#3
Re: XML message generation
Sorry for my bad English, cilu. :-)
I mean I need to generate some XML message, but I can not use XMLSerializer directly since it is not XML message to/from object mapping -- I need to mesh up information from a couple of objects together to generate XML message.
Which approach do you suggest I should use?
Originally Posted by cilu
I don't understand what you are trying to say. Can you explain it again? (or give an example?)
regards,
George
-
May 8th, 2008, 09:38 AM
#4
Re: XML message generation
Maybe the XmlWriter class can help you. Have a look at it here .
Useful or not? Rate my posting. Thanks.
-
May 8th, 2008, 01:02 PM
#5
Re: XML message generation
What do you mean by 'some XML message'? Show an xml snippet of what you want to end up with.
-
May 9th, 2008, 01:58 AM
#6
Re: XML message generation
Thanks torrud,
I am trying XMLTextWriter and XMLDocument to perform XML writing task. Any pros and cons compared between them?
Originally Posted by torrud
Maybe the XmlWriter class can help you. Have a look at it here .
regards,
George
-
May 9th, 2008, 02:27 AM
#7
Re: XML message generation
Sorry for my bad English, Arjay!
Just want to generate some strings (in XML format like this), "<Student type='regular'><Name>Tommy</Name></Student>", I have the information of student name -- "Tommy" and type "regular". I have also the format of element's layout.
Any recommendations?
Originally Posted by Arjay
What do you mean by 'some XML message'? Show an xml snippet of what you want to end up with.
regards,
George
-
May 9th, 2008, 02:35 AM
#8
Re: XML message generation
Can I suggest one of the easiest methods. Put the data into a dataset and use datasets WriteXML method to generate the XML. How does that sound?
-
May 9th, 2008, 03:21 AM
#9
Re: XML message generation
Originally Posted by George2
Sorry for my bad English, Arjay!
Just want to generate some strings (in XML format like this), "<Student type='regular'><Name>Tommy</Name></Student>", I have the information of student name -- "Tommy" and type "regular". I have also the format of element's layout.
Any recommendations?
regards,
George
I'd create an xsd of the xml and then run the XSD.exe tool to generate a Student class for the xml. Then I'd modify the class to add some XmlSerialization functionality (contained within the CreateXml() method).
Code:
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace XmlStudent
{
class Program
{
static void Main( string[ ] args )
{
string xml = Student.CreateXml( "Tommy", "regular" );
Console.WriteLine( xml );
}
}
/// <remarks/>
[XmlTypeAttribute(AnonymousType=true)]
[XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Student
{
public static string CreateXml( string name, string type )
{
Student student = new Student( name, type );
return student.CreateXml( );
}
public Student( ) { }
private Student( string name, string type )
{
_name = name;
_type = type;
}
/// <remarks/>
public string Name { get { return _name; } set { _name = value; } }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute( )]
public string type { get { return _type; } set { _type = value; } }
protected string CreateXml( )
{
XmlSerializer serializer = new XmlSerializer( typeof( Student ) );
StringBuilder sb = new StringBuilder( );
XmlWriterSettings xws = new XmlWriterSettings( );
xws.Indent = false;
xws.OmitXmlDeclaration = true;
using( XmlWriter writer = XmlTextWriter.Create( sb, xws ) )
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces( );
ns.Add( "", "" );
serializer.Serialize( writer, this, ns );
return sb.ToString( );
}
}
private string _name;
private string _type;
}
}
The output is:
Code:
<Student type="regular"><Name>Tommy</Name></Student>
-
May 9th, 2008, 05:06 AM
#10
Re: XML message generation
Thanks Shuja,
I think you mean this class,
http://msdn.microsoft.com/en-us/libr...et(VS.80).aspx
I have used ADO.Net before, but to use DataSet, I have to put the data into a database table? Read it and then write to XML? Does it what you mean?
Originally Posted by Shuja Ali
Can I suggest one of the easiest methods. Put the data into a dataset and use datasets WriteXML method to generate the XML. How does that sound?
regards,
George
-
May 9th, 2008, 05:09 AM
#11
Re: XML message generation
Sorry Arjay,
I think your method works but not working for my situation. In my situation, the class is ready and I want to generate XML message (on the other hand of your situation, when we have XML message but no class).
I think in this situation, I should use XMLTextWriter or XMLDocument, right? What are the pros and cons between them from functional and performance perspective?
Originally Posted by Arjay
I'd create an xsd of the xml and then run the XSD.exe tool to generate a Student class for the xml. Then I'd modify the class to add some XmlSerialization functionality (contained within the CreateXml() method).
...
Code:
<Student type="regular"><Name>Tommy</Name></Student>
regards,
George
-
May 9th, 2008, 05:15 AM
#12
Re: XML message generation
Originally Posted by George2
No to add data into a dataset you do not necessarily need a database. DataSet is an in-memory representation of the database. You can create Datatables, etc in the dataset and add data to it, without even linking the dataset to any database. And later you can call the WriteXML method.
Remember DataSet is disconnected in nature.
-
May 9th, 2008, 05:54 AM
#13
Re: XML message generation
Originally Posted by George2
In my situation, the class is ready and I want to generate XML message (on the other hand of your situation, when we have XML message but no class).
George the xmlserializer works both ways. Given a class instance it can produce xml; given xml it can create an instance of a class. Btw, drop the use of the word 'message' - it is confusing. So if you already have a class, decorate it with the appropriate XmlAttributes and run it through the XmlSerializer to produce the xml.
-
May 11th, 2008, 02:54 AM
#14
Re: XML message generation
Thanks Shuja,
I have found good samples for XMLDocument, XMLSerializer and XMLTextReader/XMLTextWriter.
I also find good samples about how to use Dataset with database table, and dump the information into XML message.
But, I do not find how to use Dataset without using database tables and I am interested to learn this technology from you. Do you have any good referred documents?
Originally Posted by Shuja Ali
No to add data into a dataset you do not necessarily need a database. DataSet is an in-memory representation of the database. You can create Datatables, etc in the dataset and add data to it, without even linking the dataset to any database. And later you can call the WriteXML method.
Remember DataSet is disconnected in nature.
regards,
George
-
May 13th, 2008, 03:50 AM
#15
Re: XML message generation
Have a look at the DataTable class. In the MSDN Library you can see how to create a complete dataset in code without a database.
Useful or not? Rate my posting. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|