XmlWriterSettings settings = gcnew XmlWriterSettings(); - No copy constructor?

H

hedbonker

OK - I am new to .net C++. Trying to write a simple app that creates
an XML output file based on some values that a user puts in a form.
After looking in the help, the sample code provided was this:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
using (XmlWriter writer = XmlWriter.Create("personal.xml", settings))
{
// Write XML data.
writer.WriteStartElement("Personal Information");
writer.WriteElementString("First Name", txtFname->Text);
writer.WriteElementString("Last Name", txtLname->Text);
writer.WriteElementString("E-mail", txtEmail->Text);
writer.WriteEndElement();
writer.Flush();
}

The element strings were replaced by the values on my form. Other then
that, this was the example. I also added the namespace reference to
the namespace region of my code like this: using namespace
System::Xml;

After getting a ton of errors and going back to look at the example in
the help, I noticed that it was C# and not C++ (duh).

Can anyone help me to understand what I need to do to make this work in
C++?

Thanks in advance!
 
H

hedbonker

OK, I scratched that code and used this:

XmlTextWriter^ DataWrite; // Performs the actual data write.

DataWrite = gcnew XmlTextWriter("C:\NTIXMLfile.xml",
System::Text::Encoding::UTF8);

DataWrite->WriteStartDocument(true);
DataWrite->WriteWhitespace("\r\n");

DataWrite->WriteComment("Test XML file.");
DataWrite->WriteWhitespace("\r\n");

DataWrite->WriteStartElement("Personal Information");
DataWrite->WriteWhitespace("\r\n");

DataWrite->WriteElementString("First Name", txtFname->Text);
DataWrite->WriteWhitespace("\r\n");

DataWrite->WriteElementString("Last Name",txtLname->Text);
DataWrite->WriteWhitespace("\r\n");

DataWrite->WriteElementString("E-Mail Address", txtEmail->Text);
DataWrite->WriteWhitespace("\r\n");

DataWrite->WriteEndElement();

DataWrite->WriteEndDocument();
DataWrite->Close();

This works fine. Couple of questions though, VCPP wanted me to use an
object handle (^) instead of an object pointer (*). Why is this?
Additionally it wanted me to use the garbage collection version of new
(gcnew) instead of the standard new. Why is that?

Also, the XML that is generated is not indented. Is there a way to
foce indentation in the XML file?

And finally, is there somewhere that discusses the object handle in
detail that I can look at? An object pointer is familiar to me. This
new ^ handle is not. Is this basically the same as handles were back
in the win32 days where I would get a handle to an object such as a
window or control or what have you?

Thanks in advance!
 
H

Holger Grund

OK - I am new to .net C++. Trying to write a simple app that creates
an XML output file based on some values that a user puts in a form.
After looking in the help, the sample code provided was this:

XmlWriterSettings settings = new XmlWriterSettings();
This is a reference type. In C++/CLI the direct equivalent would
be XmlWriterSettings^ settings = gcnew XmlWriterSettings; // () optional

where ^ acts like * because the concepts are similar. Access
instance members via -> not .

However, I would prefer to write
XmlWriterSettings settings;

which automatically invokes new behind the scenes and allows
access via .
settings.Indent = true;
settings.IndentChars = (" ");
using (XmlWriter writer = XmlWriter.Create("personal.xml", settings))
{
C++ has a generic concept of scope-based resource management
(RAII). The language designers apparently thought it'd be a good idea
to map the destructor to the Dispose pattern automatically. If you
could use a construct (i.e. not a static Create method) it would be as easy
as

XmlWriter writer("personal.xml",settings);

However, with Create you would need a copy constructor to make

XmlWriter writer = *XmlWriter::Create(..);

work.

Anyway, what you want in that case is auto_handle:
(Even though, I'd be careful. Looks like there are quite some
odd design decisions in there)

#include <msclr/auto_handle.h>
auto_handle<XmlWriter> writer = XmlWriter::Create(..);

The destructor will invoke Dispose on the wrapped object
as soon as writer goes out of scope.
// Write XML data.
writer.WriteStartElement("Personal Information");
Since auto_handle overload operator-> you need to use -> here.
writer.WriteElementString("First Name", txtFname->Text);

-hg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top