Creating an XML File |
|
|
|
Creating an XML File With Visual Studio .NET Without a Project |
Microsoft Visual Studio .Net provides its own means of creating an XML file. Like any other XML file, Visual Studio doesn't control what you do with the file nor does it control where and when you can use the file. Whether you are working on a project or not, you can start creating an XML file any time. If you are not working on any project, for example if you had just started Visual Studio and no project is opened, to create an XML file, on the main menu, you can click File -> New -> File... In the Templates section, you can click XML File: After clicking Open, a new file named XMLFile1 would be created. Internally, the file would have the .xml extension. If you add another XML file using the same technique, it would be called XMLFile2 or the incremental number.
|
Creating an XML File With Visual Studio .NET Without a Project |
If you are already working on a project, to start an XML file, on the main menu, you can click Project -> Add New Item... Then, in the Templates section, click XML File (.xml). You would be required to give a name to the file: After typing a name in the Name box, you can click Open. A newly added XML file in Visual Studio .Net has a line of code as: <?xml version="1.0" encoding="utf-8"?> Under this file, you can add the necessary elements and nodes. Here is an example: |
Creating XML Code Using XmlDocument |
The Microsoft .Net Framework provides the XmlDocument class that you can use to create XML code. This technique allows you to create the contents of a possible XML file as one string and use it as you wish. To create XML code using XmlDocument, this class has a method called LoadXml(). Its syntax is simply: public virtual void LoadXml(string xml); This method takes a String value as argument. The XmlDocument::LoadXml() method doesn't create an XML file, it only allows you to provide or create XML code. The code can be created as argument. Here is an example: private void btnGenerate_Click(object sender, System.EventArgs e) { XmlDocument XDoc = new XmlDocument(); Doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<employees><employee><name>Sylvie Aronson</name>" + "<department>Accounting</department></employee>" + "<employee><name>Bertrand Yamaguchi</name>" + "<department>Corporate</department>" + "</employee></employees>"); } You can also first declare and initialize a String variable with the XML code, then pass the variable as argument to the XmlDocument::LoadXml() method. After creating the file, you can save it. The XmlDocument class provides the Save() method that comes in four overloaded versions. The simplest takes as argument the name of the file to save to. Its syntax is: public virtual void Save(string filename); Here is an example: |
private void btnGenerate_Click(object sender, System.EventArgs e) { XmlDocument XDoc = new XmlDocument(); XDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<employees><employee><name>Sylvie Aronson</name>" + "<department>Accounting</department></employee>" + "<employee><name>Bertrand Yamaguchi</name>" + "<department>Corporate</department>" + "</employee></employees>"); XDoc.Save("emplrecords.xml"); }
Like all other XML file, after creating one with XDocument, you can use it as you wish. Here is the above file viewed in the MyIE2 browser: |
|
||
Home | Copyright © 2004-2010 FunctionX, Inc. | |
|