C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » XML »

Export to xml from listview Control.


Posted Date: 14 Aug 2009    Resource Type: Code Snippets    Category: XML
Author: Jyoti SharmaMember Level: Gold    
Rating: 1 out of 5Points: 10



Description :


In the listview you have items in the form of
Happy=Coding
Hello=wORLD

You want to export the listview items to xml.XML should look like:

< ?xml version="1.0" encoding="utf-8"? >
< Collection >
< Details Name="Happy" Val="Coding" / >
< Details Name="Hello" Val="World" / >
< /Collection >


Now during exporting it has to check if the file already exist or not.If it doesnot exist it should create a new xml file.And in case the file already exist then it should add the listview items before the rootnode.


private bool ExportToXML()
{
FileStream fleStream;
StreamWriter stmWriter;
XmlTextWriter xmlTxtWriter;
string filepath = Application.StartupPath + "/" + "myxml.xml";
try
{
if (File.Exists(filepath))
{
fleStream = new FileStream("myxml.xml", FileMode.Append, FileAccess.Write, FileShare.None);
stmWriter = new StreamWriter(fleStream);
xmlTxtWriter = new XmlTextWriter(stmWriter);
xmlTxtWriter.Formatting = Formatting.Indented;

XmlDocument originalXml = new XmlDocument();
stmWriter.Close();
originalXml.Load(filepath);

for (int i = 0; i < this.lstNameVal.Items.Count; i++)
{
string[] s = this.lstNameVal.Items[i].ToString().Split('=');
XmlNode coll = originalXml.SelectSingleNode("//Collection");
XmlNode det = originalXml.CreateNode(XmlNodeType.Element, "Details", null);
XmlAttribute xa = originalXml.CreateAttribute("Name");
xa.Value = s[0].ToString();
XmlAttribute xb = originalXml.CreateAttribute("Val");
xb.Value = s[1].ToString();
coll.AppendChild(det);
det.Attributes.Append(xa);
det.Attributes.Append(xb);

}
originalXml.Save(filepath);


}
else
{
fleStream = new FileStream("myxml.xml", FileMode.CreateNew, FileAccess.Write, FileShare.None);
stmWriter = new StreamWriter(fleStream);
xmlTxtWriter = new XmlTextWriter(stmWriter);
xmlTxtWriter.Formatting = Formatting.Indented;
xmlTxtWriter.WriteStartDocument();
xmlTxtWriter.WriteStartElement("Collection");

for (int i = 0; i < this.lstNameVal.Items.Count; i++)
{
string[] s = this.lstNameVal.Items[i].ToString().Split('=');
xmlTxtWriter.WriteStartElement("Details");
xmlTxtWriter.WriteAttributeString("Name", s[0].ToString());
xmlTxtWriter.WriteAttributeString("Val", s[1].ToString());
xmlTxtWriter.WriteEndElement();

}

xmlTxtWriter.WriteEndDocument();


xmlTxtWriter.Flush();
xmlTxtWriter.Close();
}
return true;
}
catch (IOException ex)
{
ex.ToString();
return false;
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Export to xml  .  Deleting items from Listview control.  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Creating XML File from the database table.
Previous Resource: Xml With C#
Return to Discussion Resource Index
Post New Resource
Category: XML


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use