Dataset data into XML doc.

T

TR

I'm just getting my feet wet with .Net;

What I'm trying to do is load field data from a dataset into an XML document
using c#

EX: Data filedName = CustID XML document element string = CustomerID

I have the dataset loaded and the xml document loaded, but can't get the
data into the proper element and save the updated XML doc.

Thanks in advance!!!!!

qb
 
S

Sahil Malik

I'd love to help you, but I can't understand this code here --

EX: Data filedName = CustID XML document element string = CustomerID

Any chance I could see your dataset contents (just do a dataset.getxml() and
paste the xml), and also your desired XML structure and what you have in the
XML doucment right now?


- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Q

qb

Thanks for the quick reply:

so I don't past too much text, here is part of the Dataset XML

<CustID>1223458</CustID>
<CustFName>Randy</CustFName>

My XML document has empty elements

<CustomerId></CustomerId>
<FirstName></FirstName>

I want to place the data into the porper element and save the XML document

Hope this is enough info for you

qb
 
S

Sahil Malik

Yes this is awesome.

Okay, the easiest possible approach since your XML and Dataset are almost a
1 to 1 mapping - name the elements in your dataset to what you expect them
to look like in the XML document, i.e. instead of
ds.Tables[0].Columns[0].ColumnName being CustID, name it to Customer ID ..
(you can alternately do this using ColumnMappings when you would have filled
your dataaset).

If that doesn't work for you, your other option is to get the data out of
the dataset as XML and then updating the nodenames via XSLT or otherwise.

Note, in this sense, you aren't really "loading" an XML document, but
creating it from the dataset.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Q

qb

Maybe this is a dumb question but how do I rename the elements in my
dataset?

What would the syntax look like for the XSLT option?

Also, is there a shorter way to type that out for each field other than
ds.Tables[0].Columns[0].ColumnName["CustID"] , like; DataTable dTable = new
DataTable(ds.Tables[0]) ?



Sahil Malik said:
Yes this is awesome.

Okay, the easiest possible approach since your XML and Dataset are almost
a 1 to 1 mapping - name the elements in your dataset to what you expect
them to look like in the XML document, i.e. instead of
ds.Tables[0].Columns[0].ColumnName being CustID, name it to Customer ID ..
(you can alternately do this using ColumnMappings when you would have
filled your dataaset).

If that doesn't work for you, your other option is to get the data out of
the dataset as XML and then updating the nodenames via XSLT or otherwise.

Note, in this sense, you aren't really "loading" an XML document, but
creating it from the dataset.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik




qb said:
Thanks for the quick reply:

so I don't past too much text, here is part of the Dataset XML

<CustID>1223458</CustID>
<CustFName>Randy</CustFName>

My XML document has empty elements

<CustomerId></CustomerId>
<FirstName></FirstName>

I want to place the data into the porper element and save the XML
document

Hope this is enough info for you

qb
 
S

Sahil Malik

qb,

There are no dumb questions :)

Well elements in a dataset can be renamed depending upon what the element
is --- a datatable can be renamed by setting the TableName property
appropriately, where as a column would have a columnname property. Rows dont
have names (logically so that is correct).

Anyway, to your other question - XSLT option -- Once your XML document is
loaded and the element names don't look quite as you wanted them to, you
could use XmlDoc.Transform(<<specify XSL transform here>>) <-- XSL
transforms are slooooooowwww and a brand new science, so this might be a
less than ideal approach for you (maybe).

The shorter way .. uhhmm .. you have no way around except to specify each
column name and data table name. You could however to shorten your code do
something like this ..

DataTable dt = ds.tables[0] ;
dt.Columns[0].ColumnName = ".." ;
dt.Columns[1].ColumnName = ".." ;
dt.Columns[2].ColumnName = ".." ;

Instead of

ds.Tables[0].Columns[0].ColumnName = ".." ;
ds.Tables[0].Columns[0].ColumnName = ".." ;
ds.Tables[0].Columns[0].ColumnName = ".." ;

So it's a little bit shorter at the expense of an extra variable (no
biggie).

BTW, might I ask, do you really care that the XML nodename should be
"CustomerID" and not "CustID", and did you look up ColumnMappings and
TableMappings??

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik




qb said:
Maybe this is a dumb question but how do I rename the elements in my
dataset?

What would the syntax look like for the XSLT option?

Also, is there a shorter way to type that out for each field other than
ds.Tables[0].Columns[0].ColumnName["CustID"] , like; DataTable dTable =
new DataTable(ds.Tables[0]) ?



Sahil Malik said:
Yes this is awesome.

Okay, the easiest possible approach since your XML and Dataset are almost
a 1 to 1 mapping - name the elements in your dataset to what you expect
them to look like in the XML document, i.e. instead of
ds.Tables[0].Columns[0].ColumnName being CustID, name it to Customer ID
.. (you can alternately do this using ColumnMappings when you would have
filled your dataaset).

If that doesn't work for you, your other option is to get the data out of
the dataset as XML and then updating the nodenames via XSLT or otherwise.

Note, in this sense, you aren't really "loading" an XML document, but
creating it from the dataset.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik




qb said:
Thanks for the quick reply:

so I don't past too much text, here is part of the Dataset XML

<CustID>1223458</CustID>
<CustFName>Randy</CustFName>

My XML document has empty elements

<CustomerId></CustomerId>
<FirstName></FirstName>

I want to place the data into the porper element and save the XML
document

Hope this is enough info for you

qb

I'd love to help you, but I can't understand this code here --

EX: Data filedName = CustID XML document element string = CustomerID

Any chance I could see your dataset contents (just do a
dataset.getxml() and paste the xml), and also your desired XML
structure and what you have in the XML doucment right now?


- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


I'm just getting my feet wet with .Net;

What I'm trying to do is load field data from a dataset into an XML
document using c#

EX: Data filedName = CustID XML document element string =
CustomerID

I have the dataset loaded and the xml document loaded, but can't get
the data into the proper element and save the updated XML doc.

Thanks in advance!!!!!

qb
 
Q

qb

Started since you mentioned them...I've noticed before...I think I need a
little more understanding of the dataset first.

So if I understnad you correctly its as simple as;

xmlDoc.NodeName.Value = dt.Columns[0].ColumnName["CustID"].ToString();

Not sure of the syntax on the xmlDcument side

qb

Sahil Malik said:
qb,

There are no dumb questions :)

Well elements in a dataset can be renamed depending upon what the element
is --- a datatable can be renamed by setting the TableName property
appropriately, where as a column would have a columnname property. Rows
dont have names (logically so that is correct).

Anyway, to your other question - XSLT option -- Once your XML document is
loaded and the element names don't look quite as you wanted them to, you
could use XmlDoc.Transform(<<specify XSL transform here>>) <-- XSL
transforms are slooooooowwww and a brand new science, so this might be a
less than ideal approach for you (maybe).

The shorter way .. uhhmm .. you have no way around except to specify each
column name and data table name. You could however to shorten your code do
something like this ..

DataTable dt = ds.tables[0] ;
dt.Columns[0].ColumnName = ".." ;
dt.Columns[1].ColumnName = ".." ;
dt.Columns[2].ColumnName = ".." ;

Instead of

ds.Tables[0].Columns[0].ColumnName = ".." ;
ds.Tables[0].Columns[0].ColumnName = ".." ;
ds.Tables[0].Columns[0].ColumnName = ".." ;

So it's a little bit shorter at the expense of an extra variable (no
biggie).

BTW, might I ask, do you really care that the XML nodename should be
"CustomerID" and not "CustID", and did you look up ColumnMappings and
TableMappings??

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik




qb said:
Maybe this is a dumb question but how do I rename the elements in my
dataset?

What would the syntax look like for the XSLT option?

Also, is there a shorter way to type that out for each field other than
ds.Tables[0].Columns[0].ColumnName["CustID"] , like; DataTable dTable =
new DataTable(ds.Tables[0]) ?



Sahil Malik said:
Yes this is awesome.

Okay, the easiest possible approach since your XML and Dataset are
almost a 1 to 1 mapping - name the elements in your dataset to what you
expect them to look like in the XML document, i.e. instead of
ds.Tables[0].Columns[0].ColumnName being CustID, name it to Customer ID
.. (you can alternately do this using ColumnMappings when you would have
filled your dataaset).

If that doesn't work for you, your other option is to get the data out
of the dataset as XML and then updating the nodenames via XSLT or
otherwise.

Note, in this sense, you aren't really "loading" an XML document, but
creating it from the dataset.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik




Thanks for the quick reply:

so I don't past too much text, here is part of the Dataset XML

<CustID>1223458</CustID>
<CustFName>Randy</CustFName>

My XML document has empty elements

<CustomerId></CustomerId>
<FirstName></FirstName>

I want to place the data into the porper element and save the XML
document

Hope this is enough info for you

qb

I'd love to help you, but I can't understand this code here --

EX: Data filedName = CustID XML document element string =
CustomerID

Any chance I could see your dataset contents (just do a
dataset.getxml() and paste the xml), and also your desired XML
structure and what you have in the XML doucment right now?


- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


I'm just getting my feet wet with .Net;

What I'm trying to do is load field data from a dataset into an XML
document using c#

EX: Data filedName = CustID XML document element string =
CustomerID

I have the dataset loaded and the xml document loaded, but can't get
the data into the proper element and save the updated XML doc.

Thanks in advance!!!!!

qb
 
S

Sahil Malik

xmlDoc.NodeName.Value = dt.Columns[0].ColumnName["CustID"].ToString();
<-- Not quite.

What I meant was ---

dt.Columns[0].ColumnName["CustID"].ColumnName = "CustomerID" ;
......
And then dt.WriteXml(...) . etc. etc.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik



qb said:
Started since you mentioned them...I've noticed before...I think I need a
little more understanding of the dataset first.

So if I understnad you correctly its as simple as;

xmlDoc.NodeName.Value = dt.Columns[0].ColumnName["CustID"].ToString();

Not sure of the syntax on the xmlDcument side

qb

Sahil Malik said:
qb,

There are no dumb questions :)

Well elements in a dataset can be renamed depending upon what the element
is --- a datatable can be renamed by setting the TableName property
appropriately, where as a column would have a columnname property. Rows
dont have names (logically so that is correct).

Anyway, to your other question - XSLT option -- Once your XML document is
loaded and the element names don't look quite as you wanted them to, you
could use XmlDoc.Transform(<<specify XSL transform here>>) <-- XSL
transforms are slooooooowwww and a brand new science, so this might be a
less than ideal approach for you (maybe).

The shorter way .. uhhmm .. you have no way around except to specify each
column name and data table name. You could however to shorten your code
do something like this ..

DataTable dt = ds.tables[0] ;
dt.Columns[0].ColumnName = ".." ;
dt.Columns[1].ColumnName = ".." ;
dt.Columns[2].ColumnName = ".." ;

Instead of

ds.Tables[0].Columns[0].ColumnName = ".." ;
ds.Tables[0].Columns[0].ColumnName = ".." ;
ds.Tables[0].Columns[0].ColumnName = ".." ;

So it's a little bit shorter at the expense of an extra variable (no
biggie).

BTW, might I ask, do you really care that the XML nodename should be
"CustomerID" and not "CustID", and did you look up ColumnMappings and
TableMappings??

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik




qb said:
Maybe this is a dumb question but how do I rename the elements in my
dataset?

What would the syntax look like for the XSLT option?

Also, is there a shorter way to type that out for each field other than
ds.Tables[0].Columns[0].ColumnName["CustID"] , like; DataTable dTable =
new DataTable(ds.Tables[0]) ?



Yes this is awesome.

Okay, the easiest possible approach since your XML and Dataset are
almost a 1 to 1 mapping - name the elements in your dataset to what you
expect them to look like in the XML document, i.e. instead of
ds.Tables[0].Columns[0].ColumnName being CustID, name it to Customer ID
.. (you can alternately do this using ColumnMappings when you would
have filled your dataaset).

If that doesn't work for you, your other option is to get the data out
of the dataset as XML and then updating the nodenames via XSLT or
otherwise.

Note, in this sense, you aren't really "loading" an XML document, but
creating it from the dataset.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik




Thanks for the quick reply:

so I don't past too much text, here is part of the Dataset XML

<CustID>1223458</CustID>
<CustFName>Randy</CustFName>

My XML document has empty elements

<CustomerId></CustomerId>
<FirstName></FirstName>

I want to place the data into the porper element and save the XML
document

Hope this is enough info for you

qb

I'd love to help you, but I can't understand this code here --

EX: Data filedName = CustID XML document element string =
CustomerID

Any chance I could see your dataset contents (just do a
dataset.getxml() and paste the xml), and also your desired XML
structure and what you have in the XML doucment right now?


- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


I'm just getting my feet wet with .Net;

What I'm trying to do is load field data from a dataset into an XML
document using c#

EX: Data filedName = CustID XML document element string =
CustomerID

I have the dataset loaded and the xml document loaded, but can't get
the data into the proper element and save the updated XML doc.

Thanks in advance!!!!!

qb
 
C

Cor Ligthert

TR

All roads goes to Rome (and in France too Paris)

So there are more ways to do what you ask,

When it was my problem (as far as I understand yours) I would probably make
an extra datatable with the names I needed and than just loop through the
sender and receiving datatable to make the new and than write that with
writeXML

Just my thought,

Cor
 

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