Hi All,
I have to create a file named (Test.xml) with the following contents in XML Format.
< xml version="1.0" >
<MyProfile xmlns="http://xyz.com">
<Root>
<Name>Harry</Name>
<Place>New York</Place>
<Country>USA</Country>
</Root>
<VehicleType>CAR</VehicleType>
<MaritalStatus>SINGLE</MaritalStatus>
<Personal>
<Age>23</Age>
<Sex>FEMALE</Sex>
</Personal>
</MyProfile>
What would be the simpler approach to do this
I thought of creating a file using createfile and start writing the contents line by line using writefile.
But this looks pretty ordinary.
I also thought of using xml API's and create an XML File which would serve the purpose.But i am not familiar
with XML and using XML API's.
If any of u can help me please provide me with a few links or suggest me what to do.
Thanks in advance

Simplest way of doing this
Junio Vitorino
hi,
OShah, that looks pretty cool.
But i am not familiar with .NET i am working on VC++. can u tell me what changes i need to do so that i can workout something similar to your code using VC++.
Thanks in advance.
KB__
There are quite a number of ways of doing this in .NET.
First, you can just print out the formatted string line by line:
System::Text::StringBuilder ^xmlString = gcnew System::Text::StringBuilder();
xmlString->AppendFormat(
"< xml version=\"1.0\" >");xmlString->AppendFormat("<MyProfile xmlns=\"http://xyz.com\">");
xmlString->AppendFormat("<Root>");
xmlString->AppendFormat("<Name>{0}</Name>", Harry->ToString()); /* etc... */
You can also do something similar in unmanaged C++.
Or you can use the XmlWriter Classes (this code outputs to a StringBuilder):
using namespace System;
using namespace System::Xml;
using namespace System::Text;
/* ... */ StringBuilder sb(1024);
XmlWriterSettings settings;
settings.CheckCharacters = true;
settings.Indent = true;
settings.Encoding = System::Text::Encoding::UTF8; try {
XmlWriter ^xmlwriter;
/* < xml version=1.0 encoding="UTF-8" > */xmlwriter = XmlWriter::Create(%sb, %settings);
xmlwriter->WriteStartDocument();
xmlwriter->WriteStartElement("MyProfile", "http://xyz.com"); /* <MyProfile xmlns="http://xyz.com"> */
xmlwriter->WriteStartElement("Root"); /* <Root> */
xmlwriter->WriteElementString("Name", Harry.Name); /* <Name>Harry</Name> */
xmlwriter->WriteElementString("Place", Harry.Place); /* <Place>New York</Place> */
xmlwriter->WriteElementString("Country", Harry.Country); /* <Country>USA</Country> */
xmlwriter->WriteEndElement(); /* </Root> */
xmlwriter->WriteElementString("VehicleType", Harry.Vehicle); /* <VehicleType>CAR</VehicleType> */
xmlwriter->WriteElementString("MaritalStatus", Harry.MaritalStatus); /* <MaritalStatus>SINGLE</MaritalStatus> */
xmlwriter->WriteStartElement("Personal"); /* <Personal> */
xmlwriter->WriteElementString("Age", Harry.Age); /* <Age>23</Age> */
xmlwriter->WriteElementString("Sex", Harry.Sex); /* <Sex>FEMALE</Sex> */
xmlwriter->WriteEndDocument(); /* close remaining elements */
xmlwriter->Close(); /* Not necesary for StringBuilder outputs */ delete xmlwriter;
}
catch (System::Exception ^) {}
If you know XPath, you may also be able to use recursion and XmlWriter::WriteNode to simplify writing the subnodes (great if you've got XmlFragments).
The most subtle way involves using the WriteXml method of a System::Data::DataSet (great if you're saving database entries). But you have to create or design a DataSet first.
Modian
I find myself on the same situation.
Could you provide some guidance for a .NET based application
StallionLZ
Jim at bernco
Do you want to know how to write XML in unmanaged C++
If you are in a hurry, the first approach you suggested (writing XML with a bunch of _ftprintfs) would be the best bet (some of the simpler, free XML libraries do no more than this). This is the simplest of doing this (that was what you asked for
).
Alternatively, you can wrap the above .NET function into a class library, and call into it when you have to. If you find that the user cannot use that class library (eg. they don't have .NET 2 installed), then you can simply disable the XML feature.
You can use MSXML (provided with the PSDK), or you can use an open source XML library (xerces, expat, tinyXML, Altova XML, ...). If you're choosing MSXML, it helps if you've had DHTML (DOM) experience, as well as Compiler COM experience. For an example, see http://msdn.microsoft.com/library/default.asp url=/library/en-us/xmlsdk/html/404d7d8b-ba4a-4ddb-8db9-d5a126a65e7d.asp (please take a look at the help of each API used).