Home > Software design >  problem when parsing from xml to a dataTable
problem when parsing from xml to a dataTable

Time:07-29

when i parsing xml file and save it into a DataTable all the rows save on one column how to make it save every classs on one column on the DataTable

sample from the code

       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");
                    
                    while (reader.Read())
                    {
                        if (reader.IsStartElement())
                        {
        
                            switch (reader.Name.ToString())
                            {
                                case "EventID":
        
        data.EventID= reader.ReadInnerXml();
        dt.Rows.Add(data.EventID);
                                   
                                    break;
                                case "Computer":
        data.Computer= reader.ReadInnerXml();
        dt.Rows.Add(data.Computer);
                                    break;
                                case "Data":
        
                                    if (reader.GetAttribute("Name") == "TargetUserName")
           data.TargetUserName= reader.ReadInnerXml();
        dt.Rows.Add(data.TargetUserName);
                            else if (reader.GetAttribute("Name") =="TargetDomainName")
                                    
                                        data.TargetDomainName= reader.ReadInnerXml();
        
                                                 dt.Rows.Add(data.TargetDomainName);
                                                
                                    break;
Console.WriteLine(string.Join("\r\n", dt.Rows.Cast<DataRow>().Select(r => string.Join(", ", r.ItemArray))));

and i try to parsing directly to the dataTable but its given me an empty result so i try to save it into classes first then from classes to a dataTable and its work good but i don't know the syntax for decide which column to insert

the output:

5136, , , , , , , , , , , , , , , , , , , , ,
851215101, , , , , , , , , , , , , , , , , , , , ,
5136, , , , , , , , , , , , , , , , , , , , ,
851215100, , , , , , , , , , , , , , , , , , , , ,
5136, , , , , , , , , , , , , , , , , , , , ,
851215099, , , , , , , , , , , , , , , , , , , , ,
5136, , , , , , , , , , , , , , , , , , , , ,
851215098, , , , , , , , , , , , , , , , , , , , ,
5136, , , , , , , , , , , , , , , , , , , , ,
851215097, , , , , , , , , , , , , , , , , , , , ,
5136, , , , , , , , , , , , , , , , , , , , ,
851215096, , , , , , , , , , , , , , , , , , , , ,
5136, , , , , , , , , , , , , , , , , , , , ,
851215095, , , , , , , , , , , , , , , , , , , , ,
5136, , , , , , , , , , , , , , , , , , , , ,
851215094, , , , , , , , , , , , , , , , , , , , ,
5136, , , , , , , , , , , , , , , , , , , , ,

what im looking for :

 5136, 851215098, , , , , , , , , , , , , , , , , , , ,
    851215101, 5136, , , , , , , , , , , , , , , , , , , ,
   ,
    
    851215097,5136 , , , , , , , , , , , , , , , , , , ,
    851215096,5136 , , , , , , , , , , , , , , , , , , , ,
    
    851215095,5136 , , , , , , , , , , , , , , , , , , , ,
    
    851215094,5136 , , , , , , , , , , , , , , , , , , , ,
    

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">%4</Data></EventData></Event>
</eventxml>

problem solved by @jdweng

and for me if u have this error ('Value cannot be null. (Parameter 'element')) try to make the first two lines like the latest like this Int EventID = (Int)xEvent.Descendants("EventID").FirstOrDefault();

CodePudding user response:

Use Xml Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication36
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("EventID",typeof(int));
            dataTable.Columns.Add("Computer",typeof(string));
            dataTable.Columns.Add("TargetUserName",typeof(string));
            dataTable.Columns.Add("TargetDomainName",typeof(string));

            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement xEvent in doc.Descendants("Event"))
            {
                int EventID = (int)xEvent.Element("EventID");
                string Computer = (string)xEvent.Element("Computer");
                string TargetUserName = (string)xEvent.Descendants("Data").Where(x => (string)x.Attribute("Name") == "TargetUserName").FirstOrDefault();
                string TargetDomainName = (string)xEvent.Descendants("Data").Where(x => (string)x.Attribute("Name") == "TargetDomainName").FirstOrDefault();

                dataTable.Rows.Add(new object[] { EventID, Computer, TargetUserName, TargetDomainName });
            }

        }
    }
}
  • Related