Reputation: 309
I am preparing for an exam from Software Development Fundamentals and I have a question I am not sure about. Could somebody tell me, preferable with some explanation suitable for novice, which code syntax should I use to export given table in specific XML format:
You need to develop a C# program that exports the contents of the Customers table to an XML file. The exported data must be in the following format:
<Customer CustomerID="ALFKI" ContactName="Maria Anders" Phone="030-007-4321" /> <Customer CustomerID="ANATR" ContactName="Ana Trujillo" Phone="(5) 555-4729" />
Which of the following code segments should you use to export the Customers table to the specified XML format?
Select one:
a.
foreach(DataColumn c in dataset1.tables["Customers"].Columns)
{
c.ColumnMapping = MappingType.Element;
}
dataSet1.WriteXml("Customers.xml");
b.
foreach(DataColumn c in dataset1.tables["Customers"].Columns)
{
c.ColumnMapping = MappingType.Attribute;
}
dataSet1.WriteXml("Customers.xml", XmlWriteMode.WriteSchema);
c.
foreach(DataColumn c in dataset1.tables["Customers"].Columns)
{
c.ColumnMapping = MappingType.Element;
}
dataSet1.WriteXml("Customers.xml", XmlWriteMode.WriteSchema);
d.
foreach(DataColumn c in dataset1.tables["Customers"].Columns)
{
c.ColumnMapping = MappingType.Attribute;
}
dataSet1.WriteXml("Customers.xml");
Thank you in advance for your help!
Upvotes: 1
Views: 293
Reputation: 1172
ColumnMaping
says how are the data written in the XML. If you choose Attribute
, the are written as attributes like this
<Customer CustomerID="ALFKI" ContactName="Maria Anders" Phone="030-007-4321" />
If you choose Element
it will be written as elemens like this:
<Customer>
<CustomerID>ALFKI</CustomerID>
....
</Customer>
Two mentioned overloads of WriteXml
are documented here for two arguments overload and here for one. Here is the documentation for XmlWriteMode
.
In your case MappingType.Attribute
must be used and you want to write the data not the schema, so calling dataSet1.WriteXml("Customers.xml");
is right here.
So I believe that the right answer here is d.
Upvotes: 1