DataSet.ReadXml not getting all data - c#

I have a relatively simple xml file that I would like to use to fill a DataSet. I am trying the following code
using (DataSet ds = new DataSet())
{
ds.ReadXml(Server.MapPath("xmlFileName.xml"));
}
But my dataset ends up with only one row containing the first node of the file.
I have tried other methods such as
XmlReader xmlFile = XmlReader.Create("xmlFileName.xml", new XmlReaderSettings());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
And
FileStream fsReadXml = new FileStream("xmlFileName.xml", FileMode.Open);
XmlTextReader xmlReader = new System.Xml.XmlTextReader(fsReadXml);
newDataSet.ReadXml(xmlReader, XmlReadMode.ReadSchema);
Both of which result in empty datasets.
I can't post the entire xml file, but its format is essentially
<service_orders count="70">
<service_order order_number="1111" id="111111">
<customer>
<customer_id>55555</customer_id>
<first_name>John</first_name>
<last_name>Doe</last_name>
<email>JohnDoe#gmail.com</email>
<phone1>55555555</phone1>
The first method I mentioned only generates two columns from this
"service_order_id" and "count"
with values 0 and 70 respectively.
It seems like it's only hitting the first node?
So I'm not sure what I'm doing wrong with these methods. Is the xml file not formatted properly? Do I somehow need to make it go deeper into the nodes? Is there any way I can specify which nodes to hit?
Any help would be appreciated,
Thank you

I realized I had forgotten that DataSets hold multiple tables, and that I was only looking at the first one which contained the root. Though this did help me understand how the parser navigates the xml tree. Only leaf elements (with no children) get added to the tables, all other elements get tables of their own in the dataset.

Related

Displaying certain elements from .xml files into a DataGridView in C#?

First off, I'm absolutely new to coding so this might be really trivial, but I couldn't find a solution for this anywhere.
So, I've already got an OpenFileDialog working. I can select the xml files just fine, and it also gives me the path to the selected file. The xml files that I'm talking about contain all sorts of stuff, but I only need to extract two elements from them (zip codes and their corresponding IDs). I've also already created a DataGridView, but I just can't figure out how to make the Grid display my elements. Also, only one xml file will be used at a time.
I'm really sorry about that stupid question, but I've been trying to get this working for hours now.
You might try to read xml to dataset
var xmlFile = XmlReader.Create("File.xml", new XmlReaderSettings());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
and then bind needed values with the DataGridView:
YourDataGridView.DataSource = ds.Tables[0].Select(o => new
{
Column1 = o.SomeValue,
Column2 = o.SomeOtherValue
}).ToList();
Hope it helps

ReadXML data and Schema in DataTable

I have two strings. One string is having XML Data and another string is having corresponding XML Schema. I am trying to read the data in DataTable. It looks like it is not possible. I don't want to use dataset. Is there a way I can combine the XML data and Schema into a memory stream and read?
Put simply, no, there is not a way to load xml directly into a DataTable through methods on DataTable nor is there a way to create a DataTable directly from an arbitrary schema. Such operations must be done through DataSet; otherwise, you end up doing some very involved workarounds.
There are some techniques you could apply using xml serialization that would be able to recreate a dataset from previously serialized xml. This does not allow for the use of an arbitrary schema though.
You could also write code specifically that loads your XML (via XDocument, XmlDocument, or XmlTextReader) and creates a DataTable on the fly, but it's not trivial to write and would likely take you quite some time. It's also kind of reinventing the wheel.
Essentially, the DataSet is the only class in that hierarchy with methods to process XML because Xml could contain any number of tables. To handle the broadest number of cases when you can make almost no assumptions about the XML, it has to be implemented at that level.
You could also consider whether it's appropriate to simply load the xml into an XDocument, validate it using the Validate extension method, and use Linq to Xml to query it.
Is there a way I can combine the XML data and Schema into a memory
stream and read?
Method
static DataTable ParseXML(string xmlString)
{
DataSet ds = new DataSet();
byte[] xmlBytes = Encoding.UTF8.GetBytes(xmlString);
Stream memory = new MemoryStream(xmlBytes);
ds.ReadXml(memory);
return ds.Tables[0];
}
Example:
string xml = new XElement("inventory",
new XElement("item",
new XElement("name", "rock"),
new XElement("price", "5000")),
new XElement("item",
new XElement("name", "new car"),
new XElement("price", "1"))).ToString();
DataTable dt = ParseXML(xml);
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
Console.Write(row[col.ColumnName] + " | ");
Console.WriteLine();
}

Merge two XML files, one of which is non-conformant, in C#

I have two XML files which need to be merged into one file. When I try to merge them, I get an error saying that one of them does not conform.
The offending XML file looks something like:
<letter>
<to>
<participant>
<name>Joe Bethersonton</name>
<PostalAddress>Apartment 23R, 11454 Pruter Street</PostalAddress>
<Town>Fargo, North Dakota, USA</Town>
<ZipCode>50504</ZipCode>
</participant>
</to>
<from>
<participant>
<name>Jon Doe</name>
<PostalAddress>52 Generic Street</PostalAddress>
<Town>Romford, Essex, UK</Town>
<ZipCode>RM11 2TH</ZipCode>
</participant>
</from>
</letter>
I am trying to merge the two files using the following code snippet:
try
{
Dataset ds = new DataSet();
Dataset ds2 = new DataSet();
XmlTextReader reader1 = new XmlTextReader("C:\\File1.xml");
XmlTextReader reader2 = new XmlTextReader("C:\\File2.xml");
ds.ReadXml(reader1);
ds2.ReadXml(reader2);
ds.Merge(ds2);
}
catch(System.Exception ex)
{
Console.WriteLine(ex.Message);
}
This gives the following error:
The same table 'participant' cannot be the child table in two nested relations.
The two XML files are both encoded in UTF-16, which makes combining them by a simple text read and write difficult.
My required end result is one XML file with the contents of the first XML file followed by the contents of the second XML file, with a and tag around the whole lot and a header at the top.
Any ideas?
Thanks,
Rik
In my opinion, the XML you provided is just fine. I suggest, you use the following code and don't use the Dataset class at all:
XDocument doc1 = XDocument.Load("C:\\File1.xml");
XDocument doc2 = XDocument.Load("C:\\File2.xml");
var result = new XDocument(new XElement("Root", doc1.Root, doc2.Root));
result will contain a XML document with "Root" as the root tag and then the content of file 1 followed by the content of file 2.
Update:
If you need to use XmlDocument, you can use this code:
XmlDocument doc1 = new XmlDocument();
XmlDocument doc2 = new XmlDocument();
doc1.Load("C:\\File1.xml");
doc2.Load("C:\\File2.xml");
XmlDocument result = new XmlDocument();
result.AppendChild(result.CreateElement("Root"));
result.DocumentElement.AppendChild(result.ImportNode(doc1.DocumentElement, true));
result.DocumentElement.AppendChild(result.ImportNode(doc2.DocumentElement, true));
I suspect the solution is to provide a schema. DataSet.Merge doesn't know what to do with two sets of elements with the same name. It attempts to infer a schema, but that doesn't work out so well here.
According to this thread on MSDN, this is a limitation of the DataSet class:
The DataSet class in .NET 2.0 (Visual Studio 2005) still has the limitation of not supporting different nested tables with the same name. Therefore you will have to introduce an XML transform to pre-process the XML (and schemas) before you load them up into the DataSet.
Of course, the way that's phrased makes it seem like a newer version might have fixed this. Unfortunately, that may not be the case, as the original answer was posted back in 2005.
This knowledge base article seems to indicate that this behavior is "by design", albeit in a slightly different context.
A better explanation of why this behavior is occurring is also given on this thread:
When ADO reads XML into a DataSet, it creates DataTables to contain each type of element it encounters. Each table is uniquely identified by its name. You can't have two different tables named "PayList".
Also, a given table can have any number of parent tables, but only one of its parent relations can be nested - otherwise, a given record would get written to the XML multiple times, as a child of each of its parent rows.
It's extremely convenient that the DataSet's ReadXml method can infer the schema of the DataSet as it reads its input, but the XML has to conform to certain constraints if it's going to be readable. The XML you've got doesn't. So you have two alternatives: you can change the XML, or you can write your own method to populate the DataSet.
If it were me, I'd write an XSLT transform that took the input XML and turned PayList elements into either MatrixPayList or NonMatrixPaylist elements. Then I'd pass its output to the DataSet.
Using XmlDocument or XDocument to read in and manipulate the XML files is another possible workaround. For an example, see Merging two xml files LINQ
I found a solution using Serialization to first infer the schema,
then serialize the schema and remove the relationships contraints (this tricks the DataSet into thinking that IT has created the dataset.), then load this new schema into a DataSet.
This new dataset will be able to load both your xml files.
More details behind this trick:
Serialization Issue when using WriteXML method

C# XmlTextReader and DataSet causing Duplicate Name Exception

I'm trying to read a standard CNN news feed to put into a table, and it's telling me "duplicate 'link' column exception." on the line:
cnnds.ReadXml(CNNfeed);
Here's the whole code, and it stops the code and throws errors, when it should simply just ignore duplicate columns or use the last column.
XmlTextReader CNNfeed = new XmlTextReader("http://rss.cnn.com/rss/cnn_topstories.rss");
DataSet cnnds = new DataSet("CNN");
cnnds.ReadXml(CNNfeed, XmlReadMode.Auto); // read the XML feed
DataTable CNNNewsFeedTable = new DataTable("CNNNewsFeed");
How do I resolve this issue? I've tried everything, and the only way to get this to work is to properly not use the CNN feed.
I just changed XmlReadMode.ReadSchema and it got through this part but then it says cnnds.Tables[1] is an index out of range. Like as if it's an empty XML.
Is there any easier way to read RSS feeds from other websites without all these exceptions and problems?
Edit: It seems adding a try { } catch() around it, however redundant, seems to bypass this problem.
If you want to read the xml in a dataset, then you need the xml-schema (else ReadXml() can't distinguish between the different namespaces).
Use:
var CNNfeed = new XmlTextReader("http://rss.cnn.com/rss/cnn_topstories.rss");
var cnnds = new DataSet("CNN");
cnnds.ReadXmlSchema("http://www.thearchitect.co.uk/schemas/rss-2_0.xsd"); // read the rss schema
cnnds.ReadXml(CNNfeed); // read the XML feed
But I think you'd better use xpath to find the information you need:
var doc = XDocument.Load("http://rss.cnn.com/rss/cnn_topstories.rss");
foreach (XElement node in (IEnumerable) doc.XPathEvaluate("//item"))
{
Console.WriteLine(node.XPathSelectElement("title").Value);
}

Is there a way to take an XmlNode and display its data in a datagrid view?

I have spent about half a day searching for an answer to this question and am slowly becoming frustrated. I am working with a Web service that returns an XmlNode as its response. I would like to be able to take the XML data reference by the node and view it with a data grid view. Does any one know if this is possible?
I am using the following code:
// submit command to webserver
XmlNode response = SubmitToWebserv((XmlElement)IssueNode, state.Get);
// create XML reader to read response
XmlReader reader = new XmlNodeReader(response);
// create a data table to hold response
DataTable dt = new DataTable();
dt.ReadXmlSchema(reader);
// read data into data table
dt.ReadXml(reader);
It throws exception: DataTable does not support schema inference from Xml. The thing is, the schema is included in the XML referenced by response... So I am at a loss.
I suggest you try that using DataSet instead of DataTable. I don't know that it will work, but it makes sense that it would. The schema might have included more than one table.
OTOH, did the XmlNode include a schema at all?
I recommend you look at this in the debugger. In particular, watch to see which node the XmlReader is positioned on. I'm not 100% convinced it will advance as you want, from the wrapping element, to the schema, then to the data.
Also, is there an element wrapping the schema and data:
<node>
<xs:schema/>
<data/>
</node>
Or are the schema and data elements top-level nodes. This would be a fragment.

Categories

Resources