473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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!

Aug 2 '06 #1
3 3131
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!

Aug 2 '06 #2
<he*******@gmail.comwrote
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<XmlWriterwriter = 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
Aug 2 '06 #3
Thank you VERy much for your response. This explains a lot!

Aug 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
15
by: cedgington | last post by:
I wanted to take advantage of the large set of functionality offered by the framework, so for my latest project I'm using managed C++ with .NET v2. I'm using the gcnew operator in two different...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
10
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version....
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
3
by: pkolinko | last post by:
Hi everyone, I am writing a small low level embedded USB application using C++/CLI windows Forms. I am sort of new to the C++/CLI and having trouble understanding what happens in this very...
6
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my...
5
by: Rainer Queck | last post by:
Hello NG, Is it possible to share the settings of an application with a class libreary? In my case I have a application and a set of different reports (home made) put into a class library. The...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.