473,383 Members | 1,762 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,383 software developers and data experts.

Export DataTable to XML with simple transform

Hi

I've been looking into ways of exporting typed DataSets and DataTables to
XML files. I need to manipulate the output before creating the XML file so I
don't think the WriteXml methods will do.

I would like a simple way of transforming a DataTable from e.g.

<accounts>
<account>
<id>1</id>
<name>Bob</name>
</account>
</accounts>

to (id becomes an attribute)

<accounts>
<account id="1">
<name>Bob</name>
</account>
</accounts>

When the XML file is created I would like to validate against a schema and
attach the schema namespace. The file will then be outputted to a client
browser.

I've looked at XmlWriter, TextWriter, XmlSerializer, XslCompiledTransform
classes but I'm not sure which I should use and what the simplest approach
should be.

Any advise/documentation is appreciated.

Thanks
Andrew

Apr 4 '07 #1
5 8022
Hi Andrew,

In my opinion, you may create an XSLT file to convert the xml according to
the structure which you need and then validate the outout xml file by
XmlValidatingReader class. However, if you want to change the xml
structure, you can modify the XSLT file and not necessary to make change in
your code.

Hope this helps.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 5 '07 #2
Hi Walter

Thanks for the tip about using XSLT. I've managed to create a solution
which works for me. I have another question which is kind of related. I want
to use a XSD schema to validate a document. I have no control over the
document but it must match a schema file. I have this code so far which
works but I can't get the ValidationEventHandler to fire if the document
doesn't contain a certain root element. i.e. the root element should be call
<Accountsbut the document still passes validation when the document
contains other element names and structure. Please see the snippets below.
Can you give me some suggestions?
XmlSchemaSet sc = new XmlSchemaSet();
sc.Add(null, filePath + "AccountsImport.xsd");
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.Schemas = sc;
readerSettings.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);
XmlReader reader = XmlReader.Create(fileUpload.FileContent, readerSettings);
while (reader.Read());
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="AccountsSchema" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Accounts">
<xs:complexType>
<xs:all minOccurs="1" maxOccurs="1">
<xs:element name="Account">
<xs:complexType>
<xs:sequence>
....
Thanks
Andrew
Apr 10 '07 #3
Hi Andrew,
Thanks for your reply.

Did you mean the ValidationEventHandler cannot fire when the document
contain a wrong root element name (such as "Accountsssssssss").
The code you post in newsgroup is fine. Additionally, I have tested with it
on my side(VS 2005). However, it works fine so far...

I created a test.xml such as
<?xml version="1.0" encoding="utf-8" ?>
<Accountsssssssss>
<Account></Account>
</ Accountsssssssss >

I can get the error message said "The 'Accountsssssssss' element is not
declared."
Have I misunderstood anything here? if not, would you please also post a
xml file which works wrong on your side and tell us which version of .net
framework your application run against. I will perform more research on it.

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support

Apr 11 '07 #4
Hi Wen Yuan

I think it may be my understanding of the schema rules which may be a
problem:

if i add a meaningless xmlns attribute to the root element of the xml file
then the schema in the XmlSchemaSet doesn't test the document. At least
that's what it looks like to me.

<accountsssssss xmlns="urn:test">
<account />
</accountsssssss>

Because the xml document is created by a third party and not coming from a
trusted source I can't be sure that the document won't contain dubious
attributes. I want the schema to be very strict.

Is there a way to force the schema to test any document? I'm using VS2005,
framework v2 .

Many thanks
Andrew
Apr 11 '07 #5
Hi Andrew,
Thanks for your reply.

If the xml is not in the namespaceUri of the added schema, then the fact
that we cannot find a schema definition for the elements in the xml
instance is reported as a warning. Warnings are not turned on by default
during validation. The problem is that there is no overload of Validate
that takes in XmlSchemaValidationFlags so that you can turn on Warnings.

You can use a validating reader with the correct validation flags to work
around this issue:

XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
//validation flags
readerSettings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings;
readerSettings.Schemas = sc;
readerSettings.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);
XmlReader reader = XmlReader.Create(fileUpload.FileContent, readerSettings);
while (reader.Read());
......

Hope this helps. Please let me know if you have any further issue on this.
I'm glad to assist you.
Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support

Apr 12 '07 #6

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

Similar topics

2
by: Ghost | last post by:
Hello. What is the optimal way to manualy import/export data from/to XML file to/form my DataSet? What I wnat: 1. To add some data (records) from XML file to may dataset. 2.. To add some...
1
by: Kevin Blakeley | last post by:
I know this was just posted but I did not want this message to get lost in the other thread as it's slightly different. Yes I want to export my dataset to excel for my clients, but I don't want...
8
by: DC Gringo | last post by:
I have a simple button that should open another window and export a datagrid to an Excel file. I'm getting: "Name 'window' is not declared." What do I need to declare or import? <INPUT...
3
by: nologin | last post by:
Is it possible to export data exposed by DataView to Excel file for example ? Seba
3
by: J055 | last post by:
Hi I'd like to get a populated datatable, create an in memory xml document (file shouldn't be more than a couple of meg), load an XSLT file, do a transform and stream it to a browser. What am I...
12
by: joaotsetsemoita | last post by:
Hello everyone, im completly new to vb.net and I was assigned to do a simple piece of software that just had to select from um db in a MS access data base and insert into a SQL server Database....
19
by: cj2 | last post by:
#1 Is there a quick way to export a datatable to an excel file? Or delimited file? #2 Where is the appropriate Microsoft monitored group to ask about writing reports in SQL Reporting services...
0
by: chago | last post by:
Hi all, I am trying to adapt a program, by transforming the output part of it, so it can export to ODBC(SAS), as previously it exported to plain text. Thus I create an SqlDataAdapter "da" by...
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.