Loading XML String into datatable - c#

string parseData= "<TEST xmlns:dt=\"urn:schemas-microsoft-com:datatypes\"><A dt:dt=\"string\">10</A><B dt:dt=\"string\">20</B></TEST>";
DataSet ds = new DataSet("Whatev");
TextReader txtReader = new StringReader(parseData);
XmlReader reader = new XmlTextReader(txtReader);
ds.ReadXml(reader);
DataTable ad = new DataTable("newT");
ad.ReadXml(reader);
For this I am getting empty table in ad, and three tables in ds.
What I am expecting is,
one table with two columns A and B, and one row with values 10 and 20.
What am I doing wrong?

You're simply not using it correctly. There's a number of problems:
You are reading from the same stream twice. In the first pass, the stream pointer is at the end of the stream. You attempt to read from it again when it's already at the end so nothing else is read. Either read into your data set or into your data table, not both. Or, at least seek back to the beginning of the stream if you really want to.
Your XML is not in the correct format. It has to be in the format:
<SomeRoot>
<TableName>
<ColumnName>ColumnValue</ColumnName>
</TableName>
<TableName>
<ColumnName>AnotherColumnValue</ColumnName>
</TableName>
</SomeRoot>
You won't be able to use that method with any arbitrary XML.
Your tables do not have a schema set. You need to either read in a schema or set it up beforehand.
var table = new DataTable("TEST");
table.Columns.Add("A", typeof(string));
table.Columns.Add("B", typeof(string));
table.ReadXml(xmlReader);
Try this instead:
var xmlStr = #"<Root>
<TEST>
<A>10</A>
<B>20</B>
</TEST>
</Root>";
var table = new DataTable("TEST");
table.Columns.Add("A", typeof(string));
table.Columns.Add("B", typeof(string));
table.ReadXml(new StringReader(xmlStr));
If you decide to parse that XML yourself, LINQ can help you out here.
public static DataTable AsDataTable(XElement root, string tableName, IDictionary<string, Type> typeMapping)
{
var table = new DataTable(tableName);
// set up the schema based on the first row
XNamespace dt = "urn:schemas-microsoft-com:datatypes";
var columns =
(from e in root.Element(tableName).Elements()
let typeName = (string)e.Element(dt + "dt")
let type = typeMapping.ContainsKey(typeName ?? "") ? typeMapping[typeName] : typeof(string)
select new DataColumn(e.Name.LocalName, type)).ToArray();
table.Columns.AddRange(columns);
// add the rows
foreach (var rowElement in root.Elements(tableName))
{
var row = table.NewRow();
foreach (var column in columns)
{
var colElement = rowElement.Element(column.ColumnName);
if (colElement != null)
row[column.ColumnName] = Convert.ChangeType((string)colElement, column.DataType);
}
table.Rows.Add(row);
}
return table;
}
Then to use it:
var xmlStr = #"<Root>
<TEST xmlns:dt=""urn:schemas-microsoft-com:datatypes"">
<A dt:dt=""string"">10</A>
<B dt:dt=""string"">20</B>
</TEST>
</Root>";
var root = XElement.Parse(xmlStr);
var mapping = new Dictionary<string, Type>
{
{ "string", typeof(string) },
};
var table = AsDataTable(root, "TEST", mapping);
There probably is a better way to get the associated .NET type for a datatype but I don't know how to do that at the moment, I'll update if I find that out.

Related

empty result when parsing from xml into DataTable - c# .net

i get an empty Datatable when parsing xml file with xmlReader and import it into one DataTable , its necessary for me to save it into Data table so i can import it later into sql server with sqlbulkcopy
easily (i have huge number of lines),
what i try
using (XmlReader reader = XmlReader.Create(#"C:\Users\neyma\test/W.xml"))
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("EventID");
dataTable.Columns.Add("Computer");
dataTable.Columns.Add("TargetUserName");
dataTable.Columns.Add("TargetDomainName");
DataRow myDataRow;
myDataRow = dataTable.NewRow();
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name.ToString())
{
case "EventID":
myDataRow["EventID"] = reader.ReadInnerXml();
break;
case "Computer":
myDataRow["Computer"] = reader.ReadInnerXml();
break;
case "Data":
if (reader.GetAttribute("Name") == "TargetUserName")
myDataRow["TargetUserName"] = reader.ReadInnerXml();
else if (reader.GetAttribute("Name") == "TargetDomainName")
myDataRow["TargetDomainName"] = reader.ReadInnerXml();
break;
sample from xml
<eventxml>
<Event <EventID>36</EventID><Computer>NH</Computer><EventData><Data Name="TargetUserName">TER.go</Data><Data Name="TargetDomainName">%4</Data></EventData></Event>
<Event <EventID>51</EventID><Computer>NQ-RS1-.ov</Computer><EventData><Data Name="TargetUserName">TERMSRiv</Data><Data Name="TargetDomainName">%%4</Data></EventData></Event>
<Event <EventID>536</EventID><Computer>CRS1.ov</Computer><EventData><Data Name="TargetUserName">TERRov</Data><Data Name="TargetDomainName">%%144</Data></EventData></Event>
</eventxml>
Seems like this is going to be much easier to do with XDocument and LINQ
var xDoc = XDocument.Load(path);
var table = new DataTable { Columns = {
{"EventID", typeof(string)},
{"Computer", typeof(string)},
{"TargetUserName", typeof(string)},
{"TargetDomainName", typeof(string)},
} };
foreach (var node in xDoc.Root.Elements("Event"))
{
var data = node.Element("EventData");
table.Rows.Add(
node.Element("EventID").Value,
node.Element("Computer").Value,
data.Elements().FirstOrDefault(e => e.Attribute("Name").Value == "TargetUserName").Value,
data.Elements().FirstOrDefault(e => e.Attribute("Name").Value == "TargetDomainName").Value
);
}
dotnetfiddle
Another option is to pass the whole XML into SQL and parse it using XQuery.

Fill a datagridview with specific XML node from several XML files

I have to fill a datagridview with 3 specifics XML nodes data from several XML files.
Here it´s an example:
<?xml version='1.0' encoding='iso-8859-1'?>
<retorno>
<mensagem>
<codigo>00001 - Sucesso</codigo>
</mensagem>
<alerta>
</alerta>
<numero_nfse>641</numero_nfse>
<serie_nfse>1</serie_nfse>
<data_nfse>08/09/2020</data_nfse>
<hora_nfse>12:16:10</hora_nfse>
<arquivo_gerador_nfse>688569.xml</arquivo_gerador_nfse>
<cod_verificador_autenticidade>03379569</cod_verificador_autenticidade>
</retorno>
I need these get 3 tags - <numero_nfse>, <data_nfse>, <cod_verificador_autenticidade> - and load them into a datagridview.
However, there are more XML files, with the same tags and I would to load all of them at the same time into a datagridview.
I wrote the code bellow and as you can see, it´s not working.
string[] arquivos = Directory.GetFiles(#"D:\Documentos\retorno");
DataSet retorno = new DataSet();
for (int j = 0; j < arquivos.Length; j++)
{
FileInfo Xmls = new FileInfo(arquivos[j]);
string caminhoXmls = Convert.ToString(Xmls);
XmlDocument retornoXml = new XmlDocument();
retornoXml.Load(caminhoXmls);
XmlNodeList retornoTags = retornoXml.GetElementsByTagName("retorno");
foreach (XmlNode xn in retornoTags)
{
string XmlNumeroNfse = xn["numero_nfse"].InnerText;
string XmlDataNfse = xn["data_nfse"].InnerText;
string XmlHoraNfse = xn["hora_nfse"].InnerText;
string XmlCodigo = xn["cod_verificador_autenticidade"].InnerText;
}
retorno.ReadXml(caminhoXmls);
dgvDadosNota.DataSource = retorno.Tables[j];
}
To clarify: I want one column for each tag. So my datagridview would be with 3 columns and as many rows as there are files in the directory. There´s only one <retorno> in each XML file.
Can anyone help me?
You are loading your multiple XML files into a DataSet with one DataTable for each file, but as explained in How to bind Dataset to DataGridView in windows application you can only bind a single DataTable to a DataGridView.
Since you have only one <retorno> node in each file, it would make sense to load the files into a DataTable with 3 columns - one each for <numero_nfse>, <data_nfse>, and <cod_verificador_autenticidade> - and one row for each file.
The following code does this:
static DataTable CreateDataTableFromRetornoXML(IEnumerable<string> fileNames)
{
var columnNames = new []
{
"numero_nfse",
"data_nfse",
"cod_verificador_autenticidade",
};
var rootName = "retorno";
var table = new DataTable();
foreach (var name in columnNames)
table.Columns.Add(name, typeof(string));
foreach (var fileName in fileNames)
{
var row = table.NewRow();
var root = XElement.Load(fileName);
var retorno = root.DescendantsAndSelf(rootName).Single(); // There should be only one.
foreach (DataColumn column in table.Columns)
{
row[column] = retorno.Element(column.ColumnName)?.Value;
}
table.Rows.Add(row);
}
return table;
}
Note I have switch to the LINQ to XML API which is generally easier to work with than the old XmlDocument API.
Demo fiddle here.

Get list of row from a DataSet

I have this dataset like this:
How can I get the string "TITLE" and "SUMMARY"?
I have this code:
string path = #"C:\Users\Pichau\Downloads\teste\data.xml";
XmlDocument x = new XmlDocument();
x.Load(path);
StringReader stream = new StringReader(x.InnerXml);
DataSet ds = new DataSet();
ds.ReadXml(stream);
foreach (var i in ds.Tables["col"].Columns)
{
Console.WriteLine(i.ToString());
}
Console.ReadLine();
But with this code I get the following output:
name
type
col_Text
row_Id
I want to access the value of "TITLE" in the "name" column
You will need to find the DataRow that corresponds to your TITLE and SUMMARY rows, and then access the values of the appropriate columns.
Try this:
foreach (DataRow row in ds.Tables["col"].Rows) {
if (row["name"] == "TITLE") {
Console.WriteLine(row["col_Text"]);
}
}

Csv DataGridView Conversion to XML Winforms

so I am working on my project and I want to write datagridview which is from a CSV file into XML file and I have achieved that but what I want to know if there is any way to sort the order view or change the outcome of XML what I want is to sort Alphabetical order from a specific column. this is my code for the saving XML file.
if (saveFileDialogXml.ShowDialog() == DialogResult.OK)
{
//Xml Alphabetical order code goes here
DataTable dst = new DataTable();
dst = (DataTable)Datagridview1.DataSource;
dst.TableName = "Data";
dst.WriteXml(saveFileDialogXml.FileName);
}
}
but the output of this is
<?xml version="1.0" standalone="yes"?>
<Item_x0020_Code>Item Code</Item_x0020_Code>
<Item_x0020_Description>Item Description</Item_x0020_Description>
<Current_x0020_Count>Current Count</Current_x0020_Count>
<On_x0020_Order>On Order</On_x0020_Order>
as you can see it even put the Hexadecimal and it just throws everything there, so I was wondering if i can reformat it the way I want it to display like removing the x0020. So I tried using LINQ to see if there was a problem with file, but I keep getting another error which says
System.Xml.XmlException: 'The ' ' character, hexadecimal value 0x20, cannot be included in a name.'
This is the LINQ code :
var xmlFile = new XElement("root",
from line in File.ReadAllLines(#"C:\\StockFile\stocklist.csv")
.Where(n => !string.IsNullOrWhiteSpace(n))
where !line.StartsWith(",") && line.Length > 0
let parts = line.Split(',')
select new XElement("Item Code",
new XElement("Test1", parts[0]),
new XElement("Test2", parts[1])
)
);
Also, I am new to C# and my first post here so please excuse the messy writing or placements.
Try following :
DataTable dst = new DataTable();
int startColumn = 5;
for(int i = dst.Columns.Count - 1; i >= startColumn; i--)
{
dst = dst.AsEnumerable().OrderBy(x => dst.Columns[i]).CopyToDataTable();
}
Sorry for the late Reply I kinda figured it out so forgot to close or mark an answer anyway if any of you run to the same thing all I did was this
// Save file dialogue XML file.
if (saveFileDialogXml.ShowDialog() == DialogResult.OK)
{
//try block to catch exception and handle it.
try
{
//Changing Data Table name to stock.
string Stock = ((DataTable)Datagridview1.DataSource).TableName;
}
//Catching the exception and handling it.
catch (Exception)
{
string es = "Please Open The File Before Saving it";
string title = "Error";
MessageBox.Show(es, title);
}
// instatiate new DataTable.
DataTable dt = new DataTable
{
TableName = "Stock"
};
for (int i = 0; i < Datagridview1.Columns.Count; i++)
{
//if (dataGridView1.Columns[i].Visible) // Add's only Visible columns.
//{
string headerText = Datagridview1.Columns[i].HeaderText;
headerText = Regex.Replace(headerText, "[-/, ]", "_");
DataColumn column = new DataColumn(headerText);
dt.Columns.Add(column);
//}
}
foreach (DataGridViewRow DataGVRow in Datagridview1.Rows)
{
DataRow dataRow = dt.NewRow();
// Add's only the columns that I need
dataRow[0] = DataGVRow.Cells["Item Code"].Value;
dataRow[1] = DataGVRow.Cells["Item Description"].Value;
dataRow[2] = DataGVRow.Cells["Current Count"].Value;
dataRow[3] = DataGVRow.Cells["On Order"].Value;
dt.Rows.Add(dataRow); //dt.Columns.Add();
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
//Finally the save part:
XmlTextWriter xmlSave = new XmlTextWriter(saveFileDialogXml.FileName, Encoding.UTF8)
{
Formatting = Formatting.Indented
};
ds.DataSetName = "Data";
ds.WriteXml(xmlSave);
xmlSave.Close();

XML Writer stored results to string [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Alternative ways to convert data table to customized XML
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Product_ID", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Product_Name", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("product_Price", Type.GetType("System.Int32")));
fillRows("hello1", dt, "product1", 1111);
fillRows("hello2", dt, "product2", 2222);
fillRows("hello3", dt, "product3", 3333);
fillRows("hello4", dt, "product4", 4444);
var xmlColumnZero = dt.AsEnumerable().Select(col => col[0].ToString()).ToArray() ; // row 0 turnovermultiplieer
var xmlRowZero = dt.Columns;
string firstColumnHeader = dt.Columns[0].ToString();
// XmlDocument xmldoc = new XmlDocument();
XmlWriter writer = XmlWriter.Create("employees.xml");
writer.WriteStartDocument();
writer.WriteStartElement("Employees");
// XmlElement first = xmldoc.CreateElement("Order");
//xmldoc.AppendChild(first);
foreach (DataColumn dc in dt.Columns )
{
if (dc.ToString() == firstColumnHeader) continue;
string firstcol = dc.ToString();
writer.WriteStartElement(firstcol);
// XmlElement colRoot = xmldoc.CreateElement(firstcol);
//first.AppendChild(colRoot);
for (int i = 0 ; i <dt.Rows.Count && i< xmlColumnZero.Length ; i++)
{
string firstrow = xmlColumnZero[i];
string dtagaga = dt.Rows[i][dc].ToString();
writer.WriteElementString(firstrow, dtagaga);
// XmlElement rowRoot = xmldoc.CreateElement(firstrow, dtagaga);
//colRoot.AppendChild(rowRoot);
}
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
I want the XMl to be stored into a string while i am creating XMLWriter.
Is there another way i can create xml out of the table
XML should look like
The xml writer method stores everything into an xml file in the program location. Would prefer a string to be saved
<Employees>
<Product_Name>
<hello1>product1</hello1>
hello2>product2</hello2>
hello3>product3</hello3>
hello4>product4</hello4>
</product_name>
<product_Price>
<hello1>1111</hello1>
hello2>2222</hello2>
hello3>3333</hello3>
hello4>4444</hello4>
</product_Price>
</Employees>
Just use overloaded method XmlWriter.Create(StringBuilder output) to create xml string. In this case all output will be written to StringBuilder instead of file:
StringBuilder builder = new StringBuilder();
XmlWriter writer = XmlWriter.Create(builder);
//... build xml here
string xml = builder.ToString();
Also you can write xml to MemoryStream with XmlWriter.Create(Stream output).
Stream stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream);
// ... build xml here
stream.Position = 0;
string xml = new StreamReader(stream).ReadToEnd();
UPDATE
Extension method below will generate your xml string. By default it uses first column as element names, but you can pass any column index for column with meta data. Also I use table name to generate "Employees" tag, so provide name when you create data table DataTable dt = new DataTable("Employees");.
public static string ToXml(this DataTable table, int metaIndex = 0)
{
XDocument xdoc = new XDocument(
new XElement(table.TableName,
from column in table.Columns.Cast<DataColumn>()
where column != table.Columns[metaIndex]
select new XElement(column.ColumnName,
from row in table.AsEnumerable()
select new XElement(row.Field<string>(metaIndex), row[column])
)
)
);
return xdoc.ToString();
}
Usage is very simple:
string xml = dt.ToXml();
Output:
<Employees>
<Product_Name>
<hello1>product1</hello1>
<hello2>product2</hello2>
<hello3>product3</hello3>
<hello4>product4</hello4>
</Product_Name>
<product_Price>
<hello1>111</hello1>
<hello2>222</hello2>
<hello3>333</hello3>
<hello4>444</hello4>
</product_Price>
</Employees>
Use a StringBuilder and create the XmlWriter using the StringBuilder instead of a file:
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
writer.WriteStartDocument();
//...
writer.WriteEndDocument();
var myXmlString = sb.ToString(); //myXmlString will contain the XML
Instead of creating a file
XmlWriter writer = XmlWriter.Create("employees.xml");
You can use String stream
StringWriter sw = new StringWriter();
XmlWriter writer = XmlWriter.Create(sw);
...
...
// sw.ToString(); // string output

Categories

Resources