Windows Develop Bookmark and Share   
 index > Windows Forms Designer > new to XML ..Write XML How ?? [:(]
 

new to XML ..Write XML How ?? [:(]

Hi
Buddies
I have a new question in XML which i don't know anything but my idea works for that
See i have a button and some text boxes on the form when a user clicks on the save button it will be saved to the database and i want to write the recent inserted data to the XML file i.e., when Save button clicked it becomes new record and i want to write the Data to the XML file which is present ....If the present XML has some data so it has to clear and then write a new file ..How can this be achieved and also update please help me out

Thanks
Avinash
Avinash Desai  Friday, July 11, 2008 12:08 PM
There are probably other ways of doing this, but the easiest way I found is to first store your textbox data in a class, then use an XmlSerializer to write the data to an xml file. The class that you write the data to must be serializable. I'm not sure of all the requirements for that but I think that you need to have an empty constructor and all data must be publically accessable. Here is an example:

class MyClass
{
string m_data1, m_data2;

public MyClass()
{
m_data1 = "";
m_data2 = "";
}

public string Data1
{
get
{
return m_data1;
}
set
{
m_data1 = value;
}
}

public string Data2
{
get
{
return m_data2;
}
set
{
m_data2 = value;
}
}
}

When you click Save, you can write your data to a class like this. Then when you want to serialize, you can do this:

private void HandleSaveButtonClick( object sender, EventArgs args )
{
// Build your object
MyClass mClass = new MyClass();
mClass.Data1 = m_textbox1.Text;
mClass.Data2 = m_textbox2.Text;

//--------------------------------------------------------------------------------------
// Open a file stream (delete the file first so we don't get mangled data when we write)
//--------------------------------------------------------------------------------------
string path = "blah/mypath/blah.xml"
File.Delete( path );
FileStream fs = new FileStream( path, FileMode.OpenOrCreate, FileAccess.Write );

//-------------------------------------
// Serialize mClass to a file
//-------------------------------------
XmlSerializer serializer = new XmlSerializer( typeof( MyClass ) );
serializer.Serialize( fs, mClass );
fs.Close();
}

Hope that helps,
Jordan
Jordan1983  Friday, July 11, 2008 1:51 PM
There are probably other ways of doing this, but the easiest way I found is to first store your textbox data in a class, then use an XmlSerializer to write the data to an xml file. The class that you write the data to must be serializable. I'm not sure of all the requirements for that but I think that you need to have an empty constructor and all data must be publically accessable. Here is an example:

class MyClass
{
string m_data1, m_data2;

public MyClass()
{
m_data1 = "";
m_data2 = "";
}

public string Data1
{
get
{
return m_data1;
}
set
{
m_data1 = value;
}
}

public string Data2
{
get
{
return m_data2;
}
set
{
m_data2 = value;
}
}
}

When you click Save, you can write your data to a class like this. Then when you want to serialize, you can do this:

private void HandleSaveButtonClick( object sender, EventArgs args )
{
// Build your object
MyClass mClass = new MyClass();
mClass.Data1 = m_textbox1.Text;
mClass.Data2 = m_textbox2.Text;

//--------------------------------------------------------------------------------------
// Open a file stream (delete the file first so we don't get mangled data when we write)
//--------------------------------------------------------------------------------------
string path = "blah/mypath/blah.xml"
File.Delete( path );
FileStream fs = new FileStream( path, FileMode.OpenOrCreate, FileAccess.Write );

//-------------------------------------
// Serialize mClass to a file
//-------------------------------------
XmlSerializer serializer = new XmlSerializer( typeof( MyClass ) );
serializer.Serialize( fs, mClass );
fs.Close();
}

Hope that helps,
Jordan
Jordan1983  Friday, July 11, 2008 1:51 PM

You can use google to search for other answers

Custom Search

More Threads

• CustomForm problem
• Custom events not showing up under Properties/Events in Designer?
• Form Inheritance and Inheritance Picker problem
• Need to create a custom calendar control
• Source Code Not Updating on ExpandableObject Converter
• Treeview - Problem with multiple languages
• write a string directly on flash drive
• Inheritance and default Text values
• When and how should a custom control be used?
• Windows Designer Bug when using ConnectionString in user control.