C# DataSet validation from XML schema - c#

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.

Related

Get data from sqlite in XML format

I have an App which communicate to a MySQL database via webservice. The webservice serves the app with XML. Now I want to replace the MySQL database with a SQLite database. To avoid changing all logic I only need to get an XML format back from my SQLite database. To increase the level of problem, I have to read data from more than one table. Details of App: For each table I have the structure of my tables in class stored and this is the code I use currently which is not working:
XmlSerializer xs = new XmlSerializer(typeof(myTableClass));
var sRe = new myTableClass();
using (XmlWriter writer = XmlWriter.Create(sww))
{
xs.Serialize(writer, sRe);
var buf = sww.ToString();
return buf;
}
I expect a string of my XML in the variable buf, but when I run this code it just jump out on the first row. What is wrong in my code?

How to Save a Generated XSD File

I have some code (in c#) that creates a bunch of XML sheets on the fly. At the end of my code I am generating XSDs based on those XML sheets. I am making the XSDs successfully, but saving them as files is what I cannot figure out. My code so far is basically taken from the MSDN page on generating XSDs from XML sheets:
Directory.CreateDirectory(directoryName);
string[] directoryFiles = Directory.GetFiles(xmlFilePath);
foreach (string xFile in directoryFiles)
{
XmlReader reader = XmlReader.Create(xFile);
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference schema = new XmlSchemaInference();
schema.TypeInference = XmlSchemaInference.InferenceOption.Relaxed;
schemaSet = schema.InferSchema(reader);
//insert code here to save the file
//stored in schemaSet.Schemas()
}
Any help is appreciated. Thanks.
XMLSchemaSet has a method called Schemas() that returns a collection of all the schemas in the set. MSDN has a page that describes how to access these.
Simply access each Schema in the set using the code in the link above and write it using the example here.

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# - Saving Dataset to XML

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);

Categories

Resources