CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: XML message generation

  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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?)
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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?

    Quote 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

  4. #4
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: XML message generation

    Maybe the XmlWriter class can help you. Have a look at it here .
    Useful or not? Rate my posting. Thanks.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,481

    Re: XML message generation

    What do you mean by 'some XML message'? Show an xml snippet of what you want to end up with.

  6. #6
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: XML message generation

    Thanks torrud,


    I am trying XMLTextWriter and XMLDocument to perform XML writing task. Any pros and cons compared between them?

    Quote Originally Posted by torrud
    Maybe the XmlWriter class can help you. Have a look at it here .

    regards,
    George

  7. #7
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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?

    Quote 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

  8. #8
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,481

    Re: XML message generation

    Quote 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>

  10. #10
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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?

    Quote 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

  11. #11
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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?

    Quote 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

  12. #12
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: XML message generation

    Quote Originally Posted by George2
    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?




    regards,
    George
    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.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,481

    Re: XML message generation

    Quote 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.

  14. #14
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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?

    Quote 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

  15. #15
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)