473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Generate XML file from C# class

Hello,

Like my question below... sorry, I hope this is a better explanation of my
question.
What I want is to generate is a XML file from my existing C# class.
I want to use this XML file for XML serialization.

How can I generate a well formed XML file from my C# code?


Thanks!

Nov 15 '05 #1
1 2083
Jax
If all you want to do is serialize an instance of class
into an XML file here is what your looking for:

//at the top
using System.Xml.Serialization;
using System.Xml;
using System.IO;

//a sample class

public class MyClass
{
public string something;
public MyClass()
{
something = "";
}
}

//in code

MyClass myClass = new MyClass();
myClass.something = "someInformation";
string path = @"c:\SomeFolder\myXmlFile.xml";
TextWriter tr = new StreamWriter(path);
XmlSerializer sr = new XmlSerializer(typeof(MyClass));
sr.Serialize(tr,m);
tr.Close();

Obviously this can be acheived in the same way with more
complex classes with more datatypes etc.
One word of warning, if your thinking of serializing a
weakly typed collection (e.g an ArrayList) you have to
specify what types of instances of classes are going to be
contained within.
This is done through XML attributes above the class, i'm
not sure of the exact syntax but i'm sure it's on the msdn
site and visual studio help (if your running that).

Hope that helps in some way.

jax
-----Original Message-----
Hello,

Like my question below... sorry, I hope this is a better explanation of myquestion.
What I want is to generate is a XML file from my existing C# class.I want to use this XML file for XML serialization.

How can I generate a well formed XML file from my C# code?


Thanks!

.

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt | last post by:
Hello, I would like to generate what I call an "overall class hierarchy" in UML automatically derived from my C++ code source. An example of what I seek: ...
2
by: PeterW | last post by:
I have an xml file from which I want to generate an xsd schema and at a later stage a cs class. The xml file has a mix of defined namespaces and also an empty namespace. These are defined as...
8
by: Bill Rust | last post by:
I've created an "Add Item" wizard for VB.NET 2003 that allows a user to add a specialized class that works with my application framework. In the wizard, the user can select the interfaces they...
8
by: Phil Lee | last post by:
I'm sure this is possible but I just can't see how to do it. I want to add schemas to my solution and have C# classes automatically generated from them. I can see how to generate typed DataSet's...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
6
by: comp.lang.php | last post by:
/** * Generate the random security image * * @access public * @param $willUseFilePath (default false) boolean to determine if you will be using a file path * @param mixed $filePath (optional)...
4
by: e_matthes | last post by:
Hello everyone, I'm trying to make a class that will generate images. I can get it to work if the class and the instance of the class is in the same file. However, I can't get it to work if I...
1
by: ergolargo | last post by:
Hello all Apologies if I've picked the wrong forum, but can anybody offer assistance to me? I'm definitely struggling to get my first data access layer generated using the Dataset Designer in...
0
by: shruthid | last post by:
can we generate a class for the following xsd file?? it does'nt contain a complex type.. <?xml version="1.0" encoding="UTF-8" ?> <xs:schema...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.