Przeklin
Przeklin

Reputation: 77

Converting XmlDocument as List of objects

At the moment I convert XmlDocument as string List but this is not a good solution becouse if I display it in ListBox this recive me inline string with all parameters and I need to display only of table then when it's checked send it Sql Server

I can't create helper class with properties because parameters can be added/delete dynamically. I don't know what the exact parameters will be in file

Here is example XML

<TechnologyTables>
   <Tables>
      <TA_ID>3102</TA_ID>
      <TA_Name>AL000-01.50M-N2-S0</TA_Name>
      <TA_MaterialID>1</TA_MaterialID>
      <TA_ThicknessID>4</TA_ThicknessID>
      <TA_IsActive>1</TA_IsActive>
      <TA_ArchiveID>100</TA_ArchiveID>
      <TA_IsArchive>0</TA_IsArchive>
      <CL_IsActive>1</CL_IsActive>
      <MP_Lens>200</MP_Lens>
      <MP_Nozzle>4.0</MP_Nozzle>
      <MP_Focal>-0.6</MP_Focal>
      <MP_NozzleDist>1.5000000e+000</MP_NozzleDist>
      <MP_GasStabilization>1</MP_GasStabilization>
      <MP_SensitiveSensor>1</MP_SensitiveSensor>
      <MP_SensitiveArea>2.5000000e+001</MP_SensitiveArea>
      <GA_ID>1</GA_ID>
      <GP_ID>0</GP_ID>
      <GP_FlushOn>1</GP_FlushOn>
      <GP_FlushTime>2000</GP_FlushTime>
      <GP_FlushPressure>1.0000000e+001</GP_FlushPressure>
      <GP_FeedbackOn>1</GP_FeedbackOn>
      <GP_FeedbackTime>0</GP_FeedbackTime>
      <GP_FeedbackPressure>1.0000000e-001</GP_FeedbackPressure>
      <GP_MaxPressure>1.0000000e+001</GP_MaxPressure>
      <GP_ContinueOn>0</GP_ContinueOn>
      <GP_ContinueTime>0</GP_ContinueTime>
      <TA_Jerk>100</TA_Jerk>
      <TA_Acceleration>100</TA_Acceleration>
      <TA_CuttingTechID>3</TA_CuttingTechID>
      <TA_FlyCut>1</TA_FlyCut>
      <TA_HeadID>1</TA_HeadID>
      <TA_MachineID>3</TA_MachineID>
      <TA_TypeID>1</TA_TypeID>
      <TT_HeadPowerID>7</TT_HeadPowerID>
      <TA_CreateDate>2019-08-26T17:10:59.810</TA_CreateDate>
      <Description>AL1.5 CATLINE</Description>
      <TA_HeadTypeID>2</TA_HeadTypeID>
      <CatlineFolder>1</CatlineFolder>
      <NozzleNameID>10</NozzleNameID>
      <LaserTypeID>1</LaserTypeID>
   </Tables>
</TechnologyTables>

Some code when importing file

private async void _ImportTechTables()
{
    var open = new OpenFileDialog();
    var TableXml = new XmlDocument();
    open.Filter = "xml files |*.xml";
    if (open.ShowDialog() == DialogResult.OK)
    {
        TableXml.Load(open.FileName);
    }
    RSA rsaKey = GetKey();
    DecryptXML(TableXml, rsaKey, "XmlKey");

    if (TableXml != null)
    {
        var import = new TechnologyTableImportViewModel();
        List<string> xmlNodeLists = new List<string>();
        XmlNodeList node = TableXml.SelectNodes("TechnologyTables/Tables");
        foreach (XmlNode nodes in node)
        {
            xmlNodeLists.Add(nodes.InnerXml);
        }
        import.List = xmlNodeLists;

And element in list look like this:

<Tables><TA_ID><Tables><TA_ID>3102</TA_ID><TA_Name>AL000-01.50M-N2<TA_Name>

Upvotes: 0

Views: 1560

Answers (2)

jdweng
jdweng

Reputation: 34421

I like using a dictionary and create dictionary using xml linq :

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


namespace ConsoleApplication137
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, string> dict = doc.Descendants("Tables").FirstOrDefault().Elements()
                .GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
}

If you have multiple tables then use following :

            List<Dictionary<string, string>> dict = doc.Descendants("Tables").Select(t => t.Elements()
                .GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault())
                ).ToList();

Upvotes: 2

Innat3
Innat3

Reputation: 3576

You can save the fields to a list this way, although I recommend using a dictionary for this kind of format.

var doc = XDocument.Parse(yourXmlFile);
var table = doc.XPathSelectElement("TechnologyTables/Tables");

var dict = new Dictionary<string, string>();

foreach (var element in table.Elements())
{
    dict.Add(element.Name.LocalName, element.Value);
}

Upvotes: 0

Related Questions