www.softwareriviera.com
Software Code
    New Code     Most Interesting     Don't Miss     Our picks  
C# - section

View Article
XMLDocument based on DragEventArgs parameter passed when dropping something into a Windows form - C# example
Create an XMLDocument based on the DragEventArgs parameter when something is dropped into a Windows form. Source code is in C#.
The code below shows you have you can create an XMLDocument object based on the DragEventArgs parameter which is passed to the DragDrop method.

This example is also great for learning how you can easily create an XMLDocument based on an object.

public static XmlDocument CreateXMLDocumentForClip(DragEventArgs e)
{
XmlNode xmlNode = null;
XmlElement xmlRootElement = null;
XmlDocument xmlDoc = new XmlDocument();
XmlNode xmlElement = null;
XmlNode xmlChildElement = null;
XmlNode xmlSecondChildElement = null;

try
{
xmlDoc.PreserveWhitespace = true;

xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmlDoc.AppendChild(xmlNode);

xmlRootElement = xmlDoc.CreateElement("", "DRAGGED_CLIP", "");

xmlElement = xmlDoc.CreateElement("", "CLIP_NAME", "");
xmlElement.InnerText = "NEW CLIP";
xmlRootElement.AppendChild(xmlElement);

xmlElement = xmlDoc.CreateElement("", "CLIP_DESCRIPTION", "");
xmlElement.InnerText = "NEW CLIP AT " + DateTime.Now.ToString();
xmlRootElement.AppendChild(xmlElement);

xmlElement = xmlDoc.CreateElement("", "CLIP_DATE", "");
xmlElement.InnerText = DateTime.Now.ToString();
xmlRootElement.AppendChild(xmlElement);

xmlElement = xmlDoc.CreateElement("", "DRAGGED_FORMATS", "");
xmlRootElement.AppendChild(xmlElement);

for (int i = 0; i < e.Data.GetFormats().Length; i++)
{
xmlChildElement = xmlDoc.CreateElement("", "DRAGGED_FORMAT", "");
xmlElement.AppendChild(xmlChildElement);

xmlSecondChildElement = xmlDoc.CreateElement("", "FORMAT_NAME", "");
xmlSecondChildElement.InnerText = e.Data.GetFormats()[i];
xmlChildElement.AppendChild(xmlSecondChildElement);

xmlSecondChildElement = xmlDoc.CreateElement("", "FORMAT_CONTENT", "");
xmlSecondChildElement.InnerText = e.Data.GetData(e.Data.GetFormats()[i]).ToString();
xmlChildElement.AppendChild(xmlSecondChildElement);
}

//
xmlDoc.AppendChild(xmlRootElement);
}
catch (Exception ex)
{
staticError = new AppError(AppDefs.ERROR_GUI_COULD_NOT_CREATE_XML_DOCUMENT_FOR_CLIP, AppDefs.ERROR_GUI_COULD_NOT_CREATE_XML_DOCUMENT_FOR_CLIP_DETAILS, ex.Message + " " + ex.StackTrace, AppDefs.ERROR_GUI_COULD_NOT_CREATE_XML_DOCUMENT_FOR_CLIP_TEXT);
return null;
}
return xmlDoc;
}
Date: 2012-5-26    Written by: Admin A
     See all articles in this section
Copyright © 2007-2009 SoftwareRiviera.com. All rights reserved.  | Terms of use | Disclaimer | Privacy Policy |