C# - Saving Dataset to XML - c#

Iam using the WriteXml() of a dataset to save the data I have in the dataset to an XML. When I save the dataset value into the XML file the format of the file is like code below.
I save the dataset like this: Order_Dataset.WriteXml(#"C:\Orders", XmlWriteMode.IgnoreSchema)
How can I write so that the XMLNS adress dose not shows in my XML file?? XmlWriteMode.IgnoreSchema should do the work but it wont
<Order_Dataset xmlns="http://tempuri.org/Order_Dataset.xsd">
<Order>
<OrderName>Coffe</OrderName>
<OrderID>1</OrderID>
<OrderDate>2011-02-20T14:11:21+01:00</OrderDate>
</Order>

Have you tried changing the namespace of DataSet before saving it?
DataSet ds = new DataSet("MyDataSet");
ds.Namespace = "";
ds.WriteXml(...);

DataSet has internal variable fTopLevelTable that is changed only if a XML file is loaded. Use debugger to see difference after you load your hand changed XML into... This does the trick if you create DataSet by code or with ReadXmlSchema().
FieldInfo fieldInfo = typeof(DataSet).GetField("fTopLevelTable", BindingFlags.NonPublic | BindingFlags.Instance);
fieldInfo.SetValue(yourDS, true);

Related

Binding the dataset from xml files cannot get the data

I need to bind the dataset from xml files after sorting it. I followed the example from Loading a DataSet from XML. But I got the error indexOutOfRangeEXception. The dataset has no table. Would someone tell me how to solve the problem.
I am able to add the node into the xml on another function in my code. It proved the file path is correct.
Thanks in advance.
there is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<EmailList>
<EmailAddress>
<ID>90</ID>
<DisplayName>TesterA</DisplayName>
<Email>TesterA#gmail.ca</Email>
</EmailAddress>
<EmailAddress>
<ID>75</ID>
<DisplayName>TesterB</DisplayName>
<Email>TesterB#gmail.ca</Email>
</EmailAddress>
<EmailAddress>
<ID>91</ID>
<DisplayName>TesterC</DisplayName>
<Email>TesterC#gmail.ca</Email>
</EmailAddress>
</EmailList>
There is my code:
Dim ds As DataSet = New DataSet
ds.ReadXml(fileName, XmlReadMode.ReadSchema)
If ds.Tables(0).Rows.Count > 0 Then
Dim dv As DataView = ds.Tables(0).DefaultView
dv.Sort = "DisplayName"
grdEmailList.DataSource = dv
grdEmailList.DataBind()
End If
Change XmlReadMode.ReadSchema to XmlReadMode.Auto or remove it. Your XML doesnot contains schema.

C# DataSet validation from XML schema

I have a xml schema. I want to populate a DataSet and validate it using this schema.
DataSet package = new DataSet();
StringReader schemaResourceReader = new StringReader(PackageValidationLibrary.Properties.Resources.myPackage);
package.ReadXmlSchema(schemaResourceReader);
package.Tables["Table1"].Rows.Add(packageDetail.date,packageDetail.code,packageDetail.amount,packageDetail.place,"0");
package.Tables["Table2"].Rows.Add("0","0");
foreach (Cek data in recordList){
package.Tables["Table3"].Rows.Add(data.Serial, data.Code, data.Branch, data.ValidityDate, "0");
}
Using the code above I can load data but I cannot validate it although dataset imports the schema.
I tried to get xml string using package.GetXml() method and reload the xml again. Then I got exceptions.
How can I validate this table? Thanks.
EDIT
As I understand from answers and comments it is not possible to validate while populating the dataset. Then I read the xml from dataset and loaded it with the configuration given below.
_schema = XmlSchema.Read(new StringReader(PackageValidationLibrary.Properties.Resources.TakasPaketi), new ValidationEventHandler(ValidationEventHandler));
XmlReaderSettings _settings = new XmlReaderSettings();
_settings.Schemas.Add(_schema);
_settings.ValidationType = ValidationType.Schema;
XmlReader vreader = XmlReader.Create(stream, _settings);
I believe this will do it:
// First, read in the XML schema
DataSet MyDataSet = new DataSet();
MyDataSet.ReadXmlSchema(#"C:\YourSchema.xsd");
// Now, read in the XML file (it is validated
// against the schema when it is read in).
MyDataSet.ReadXml(#"C:\YourFile.xml");
If you edit the data xml file to not match the schema, an exception will be thrown when the xml file is read.
So in your case you may have to export the dataset to an xml string and then read it back in. You say you are getting exceptions when you do this....what exceptions? Maybe the data isn't valid for the schema you have.

How to Read XML into a DataSet

I have a class that goes to a URL and gets a xml document using xmlDoc.Load(URL). To test the class, I added a web project to display the xml in a grid view.
In a button click I create an instance of an xml document and populate it as:
xmlDoc = myClassName()
I'm stuck at how to get xmlDoc into a format usable by the datasource
I am totally confused as to how to get the xml to be displayed in the grid as dataset.ReadXml seems to want a file path. I don't understand the other overloads. I suppose I have to read the xml into a string or something else, but I don't understand how to do this - even after reading numerous posts here and MSDN - Thanks!
Example:
string xml =#"<xml><customer><id>1</id></customer></xml>";
DataSet ds = new DataSet();
ds.ReadXml(XmlReader.Create(new StringReader(xml)));
Now set the datasource to your grid:
grid.DataSource=newDataSet.Tables[0];
Update:
DataSet ds = new DataSet();
//xmlDocument is your XmlDocument instance
ds.ReadXml(XmlReader.Create(new StringReader(xmlDocument.InnerXml)));
grid.DataSource=newDataSet.Tables[0];

Duplicate column in dataset

I have to load a response from an xml file into a dataset. I have written the following code in c#
XmlDocument doc = new XmlDocument();
doc.LoadXml(Response);
DataSet ds = new DataSet();
ds.ReadXml(new XmlNodeReader(doc));
DataTable EquoteRes = ds.Tables["EQuote"];
When I debug, I get this error:
A column named 'ChildNodes' already belongs to this DataTable:
cannot set a nested table name to the same name.
xml file is as below
How can I get rid of this error?
Very Similar discussion on the asp.net forum here.
And for more information msdn can also be referred.

C# WinForms Read XML File - Only Specific Nodes

I have an XML File:
<Database>
<SMS>
<Number>+447761692278</Number>
<DateTime>2009-07-27T15:20:32</DateTime>
<Message>Yes</Message>
<FollowedUpBy>Unassigned</FollowedUpBy>
<Outcome></Outcome>
<Quantity>0</Quantity>
<Points>0</Points>
</SMS>
<SMS>
<Number>+447706583066</Number>
<DateTime>2009-07-27T15:19:16</DateTime>
<Message>STOP</Message>
<FollowedUpBy>Unassigned</FollowedUpBy>
<Outcome></Outcome>
<Quantity>0</Quantity>
<Points>0</Points>
</SMS>
</Database>
Currently I read it into a datagridview using this:
public void Read()
{
DataSet ds = new DataSet("SMS DataSet");
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml(#"C:\Documents and Settings\Administrator\Desktop\RecSmsDB.xml");
ds = xmlDatadoc.DataSet;
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "SMS";
this.dataGridView1.Sort(dataGridView1.Columns["DateTime"], ListSortDirection.Descending);
}
I want to be able to only read in the xml objects that have a specific DateTime. Does anyone know of a way of doing this? Currently I have tried a variety of the methods included in the namespace but to no avail.
Help greatly appreciated,
regards.
***EDIT: I want to be able to dynamically change the data displayed at run time.
Not that I can think of. However, you can read all the data into the DataSet, then create a DataView which filters the SMS table, and bind your grid to that instead of the DataTable.
What if you attach the DataSet to a BindingSource and attach this BindingSource to the grid? A BindingSource has a Filter property where you can enter SQL-like expressions.

Categories

Resources