bhavanisankarnimmala

Load XML into the Class Objects

Blog Post created by bhavanisankarnimmala on Jul 2, 2018

Program.cs

---------------

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO.Compression;

using System.Xml;

using ReadAndImportGARfile.BO;

namespace ReadAndImportGARfile

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void ContBrowse_Click(object sender, EventArgs e)

{

ContainingPath.Text = Browse();

}

private void DestBrowse_Click(object sender, EventArgs e)

{

DestinationPath.Text = Browse();

}

private void btnGarImporter_Click(object sender, EventArgs e)

{

//First change file type from gar to zip

//Get the List of zip files in Array

//do unzip of the zip files into destination of decompress folder

//collect all folders contains in destination path

string[] garFilesList = Directory.GetFiles(ContainingPath.Text, "*.gar");

foreach (string garFileName in garFilesList)

{

File.Move(garFileName, Path.ChangeExtension(garFileName, ".zip"));

}

string[] zipFilesList = Directory.GetFiles(ContainingPath.Text, "*.zip");

if (this.ContainingPath.Text != "" && this.DestinationPath.Text != "" && Directory.Exists(this.ContainingPath.Text) && Directory.Exists(this.DestinationPath.Text))

{

Decompress(zipFilesList, DestinationPath.Text);

}

string[] subdirectoryEntries = Directory.GetDirectories(DestinationPath.Text);

foreach (string subdirectory in subdirectoryEntries)

{

Header header = new Header();

IList<Plat> plat = new List<Plat>();

string msAccessDBFilePath = @"D:\Bhavani_Files\IHS_Manikonda\LandTextPlacement\26thJune2018_Docs\Output2\GeostationData.mdb";

string[] xmlFilesInDir = Directory.GetFiles(subdirectory, "*.XML");

string groupFileTag = subdirectory.Split(Path.DirectorySeparatorChar).Last();

string[] xmlFilesInDirNew = new string[2];

if (Path.GetFileName(xmlFilesInDir[0].ToString().ToUpper().Trim()).EndsWith("HEADER.XML"))

{

xmlFilesInDirNew[0] = xmlFilesInDir[0];

xmlFilesInDirNew[1] = xmlFilesInDir[1];

}

else

{

xmlFilesInDirNew[0] = xmlFilesInDir[1];

xmlFilesInDirNew[1] = xmlFilesInDir[0];

}

foreach (string xmlFileName in xmlFilesInDirNew)

{

string fileName = Path.GetFileName(xmlFileName);

if (fileName.ToUpper() == "HEADER.XML")

{

string headerXMLfileName = xmlFileName;

header = ReadHeaderXML(headerXMLfileName, groupFileTag);

}

else if (fileName.ToUpper() == (groupFileTag.ToUpper() + ".XML"))

{

string platXMLfileName = xmlFileName;

plat = ReadPlatXML(platXMLfileName, groupFileTag, header.surveySystemType);

}

//Import Header & plat into the SQL Table

怀 

}

}

}

#region MyMethods

 

private string Browse()

{

FolderBrowserDialog fbd = new FolderBrowserDialog();

fbd.ShowDialog();

return fbd.SelectedPath;

}

private void Decompress(string[] zipFilesList, string destinationPath)

{

foreach (string zipFileName in zipFilesList)

{

string zipPath = zipFileName;

FileInfo file = new FileInfo(zipPath);

string fileName = Path.GetFileName(zipPath);

string extractPath = Regex.Replace(Path.Combine(destinationPath,file.Name), ".zip", "");

ZipFile.ExtractToDirectory(zipPath, extractPath);

}

}

private Header ReadHeaderXML(string headerXMLfileName, string groupFileTag)

{

Header header = new Header();

try

{

//find surveysystem from fileName

string fileName = groupFileTag.ToUpper();

header.fileName = fileName;

//Digging inside of Header.xml

XmlDocument doc = new XmlDocument();

doc.Load(headerXMLfileName);

XmlNodeList xnl = doc.SelectNodes("/Root");

foreach (XmlNode node in xnl)

{

XmlNodeList xnl2 = doc.SelectNodes("/Root/Header/Date");

foreach (XmlNode node2 in xnl2)

{

header.dateModified = node2.Attributes["modified"].InnerText;

header.dateCreated = node2.Attributes["created"].InnerText;

}

XmlNodeList xnl3 = doc.SelectNodes("/Root/Header/User");

foreach (XmlNode node3 in xnl3)

{

header.userValue = node3.Attributes["value"].InnerText;

}

XmlNodeList xnl4 = doc.SelectNodes("/Root/Header/GeoStation");

foreach (XmlNode node4 in xnl4)

{

header.geostationVersion = node4.Attributes["version"].InnerText;

}

XmlNodeList xnl5 = doc.SelectNodes("/Root/Header/Extents");

foreach (XmlNode node5 in xnl5)

{

header.ext_MinY = node5.Attributes["minimumY"].InnerText;

header.ext_MinX = node5.Attributes["minimumX"].InnerText;

header.ext_MaxY = node5.Attributes["maximumY"].InnerText;

header.ext_MaxX = node5.Attributes["maximumX"].InnerText;

}

XmlNodeList xnl6 = doc.SelectNodes("/Root/Coordinator/OtherProperties/attr");

foreach (XmlNode node6 in xnl6)

{

if (node6.Attributes["code"].InnerText.ToUpper() == "SCALE")

{

header.scale = double.Parse(node6.Attributes["value"].InnerText);

}

if (node6.Attributes["code"].InnerText.ToUpper() == "UNITS")

{

header.units = node6.Attributes["value"].InnerText;

}

}

XmlNodeList xnl7 = doc.SelectNodes("/Root/Coordinator");

foreach (XmlNode node7 in xnl7)

{

header.projection = node7.Attributes["epsgName"].InnerText;

}

int numericValue;

if (Int32.TryParse(fileName.Substring(fileName.Length - 1), out numericValue))

{

XmlNodeList xnl8 = doc.SelectNodes("/Root/Aoi/Coordinate");

foreach (XmlNode node8 in xnl8)

{

header.surveySystemType = "DLS";

header.township = node8.Attributes["township"].InnerText;

header.range = node8.Attributes["range"].InnerText;

header.meridianDir = node8.Attributes["meridianDirection"].InnerText;

header.meridian = node8.Attributes["meridian"].InnerText;

break;

}

}

else

{

XmlNodeList xnl9 = doc.SelectNodes("/Root/Aoi/Coordinate");

foreach (XmlNode node9 in xnl9)

{

header.coor_unit = node9.Attributes["unit"].InnerText;

header.surveySystemType = "NTS";

header.sheet = node9.Attributes["sheet"].InnerText;

header.primary = node9.Attributes["primary"].InnerText;

header.letter = node9.Attributes["letter"].InnerText;

header.block = node9.Attributes["block"].InnerText;

break;

}

}

}

}

catch

{

}

finally

{

}

return header;

}

private IList<Plat> ReadPlatXML(string platXMLfileName, string groupFileTag, string surveySystem)

{

IList<Plat> plat_Main = new List<Plat>();

Plat plat_layer_texts = null;

IList<string> coordinates = new List<string>();

try

{

if (surveySystem.ToUpper() == "NTS")

{

//Digging inside of Header.xml

XmlDocument xmldoc = new XmlDocument();

xmldoc.Load(platXMLfileName);

XmlNodeList layers = xmldoc.SelectNodes("/Root/Data/Layer");

foreach (XmlNode layer in layers)

{

string layerName = layer.Attributes["name"].InnerText;

string layerType = layer.Attributes["type"].InnerText;

if (layerName.ToUpper().Trim() == "OPEN CROWN LAND (C/CLU)")

{

#region Read xml for Open Crown Land (C/CLU)

XmlNodeList shapeNodes = layer.SelectNodes("Shape");

foreach (XmlNode node2 in shapeNodes)

{

string shapeType = node2.Attributes["type"].InnerText;

string shapegfsID = node2.Attributes["gfsId"].InnerText;

string textValue = node2.Attributes["text"].InnerText;

string textJustification = node2.Attributes["justification"].InnerText;

double textAngle = double.Parse(node2.Attributes["angle"].InnerText);

double textHeight = double.Parse(node2.Attributes["height"].InnerText);

double textWidth = double.Parse(node2.Attributes["width"].InnerText);

if (shapeType.ToUpper() == "TEXT")

{

XmlNodeList xnl3 = node2.SelectNodes("Point");

string keyValue = string.Empty;

foreach (XmlNode node3 in xnl3)

{

string y = node3.Attributes["y"].InnerText;

string x = node3.Attributes["x"].InnerText;

string coordinate = x + "," + y;

coordinates.Add(coordinate);

}

try

{

XmlNodeList xnl4 = node2.SelectNodes("Key");

foreach (XmlNode node4 in xnl4)

{

keyValue = node4.Attributes["value"].InnerText;

break;

}

}

catch

{

}

//finally creating the Point Object for Number of Type = Text

plat_layer_texts = new Plat();

plat_layer_texts.layerName = layerName;

plat_layer_texts.layerType = layerType;

plat_layer_texts.compositeType = "";

plat_layer_texts.composite_gfsid = "";

plat_layer_texts.shapeType = shapeType;

plat_layer_texts.gfsid = shapegfsID;

plat_layer_texts.text = textValue;

plat_layer_texts.justification = textJustification;

plat_layer_texts.angle = textAngle;

plat_layer_texts.width = textWidth;

plat_layer_texts.height = textHeight;

plat_layer_texts.coordinates = coordinates;

plat_layer_texts.keyValue = keyValue;

plat_Main.Add(plat_layer_texts);

if (coordinates != null)

{

coordinates.Clear();

}

if (plat_layer_texts != null)

{

plat_layer_texts = null;

}

}

}

#endregion

}

if ((layerName.ToUpper().Trim() == "CROWN LEASE/POSTING DEEPER") || (layerName.ToUpper().Trim() == "CROWN LEASE/POSTING"))

{

#region Read xml for Crown Lease/Posting or Deeper

XmlNodeList shapeNodes = layer.SelectNodes("Shape");

foreach (XmlNode node2 in shapeNodes)

{

string shapeType = node2.Attributes["type"].InnerText;

string shapegfsID = node2.Attributes["gfsId"].InnerText;

if (shapeType.ToUpper().Trim() == "COMPOSITE")

{

#region Composite Inner XML

XmlNodeList shapeNodes2 = node2.SelectNodes("Shape");

foreach (XmlNode node3 in shapeNodes2)

{

string shapeType_comp = node3.Attributes["type"].InnerText;

string shapegfsID_comp = node3.Attributes["gfsId"].InnerText;

怀 

if (shapeType_comp.ToUpper().Trim() == "POLYLINE")

{

string textValue = string.Empty;

string textJustification = string.Empty;

double textAngle = 0;

double textHeight = 0;

double textWidth = 0;

XmlNodeList xnl3 = node3.SelectNodes("Point");

string keyValue = string.Empty;

foreach (XmlNode node4 in xnl3)

{

string y = node4.Attributes["y"].InnerText;

string x = node4.Attributes["x"].InnerText;

string coordinate = x + "," + y;

coordinates.Add(coordinate);

}

try

{

XmlNodeList xnl4 = node3.SelectNodes("Key");

foreach (XmlNode node5 in xnl4)

{

keyValue = node5.Attributes["value"].InnerText;

break;

}

}

catch

{

}

//finally creating the Point Object for Number of Type = Text

plat_layer_texts = new Plat();

plat_layer_texts.layerName = layerName;

plat_layer_texts.layerType = layerType;

plat_layer_texts.compositeType = shapeType;

plat_layer_texts.composite_gfsid = shapegfsID;

plat_layer_texts.shapeType = shapeType_comp;

plat_layer_texts.gfsid = shapegfsID_comp;

plat_layer_texts.text = textValue;

plat_layer_texts.justification = textJustification;

plat_layer_texts.angle = textAngle;

plat_layer_texts.width = textWidth;

plat_layer_texts.height = textHeight;

plat_layer_texts.coordinates = coordinates;

plat_layer_texts.keyValue = keyValue;

plat_Main.Add(plat_layer_texts);

if (coordinates != null)

{

coordinates.Clear();

}

if (plat_layer_texts != null)

{

plat_layer_texts = null;

}

}

if (shapeType_comp.ToUpper() == "TEXT")

{

string textValue = node3.Attributes["text"].InnerText;

string textJustification = node3.Attributes["justification"].InnerText;

double textAngle = double.Parse(node3.Attributes["angle"].InnerText);

double textHeight = double.Parse(node3.Attributes["height"].InnerText);

double textWidth = double.Parse(node3.Attributes["width"].InnerText);

XmlNodeList xnl5 = node3.SelectNodes("Point");

string keyValue = string.Empty;

foreach (XmlNode node6 in xnl5)

{

string y = node6.Attributes["y"].InnerText;

string x = node6.Attributes["x"].InnerText;

string coordinate = x + "," + y;

coordinates.Add(coordinate);

}

try

{

XmlNodeList xnl4 = node2.SelectNodes("Key");

foreach (XmlNode node4 in xnl4)

{

keyValue = node4.Attributes["value"].InnerText;

break;

}

}

catch

{

}

//finally creating the Point Object for Number of Type = Text

plat_layer_texts = new Plat();

plat_layer_texts.layerName = layerName;

plat_layer_texts.layerType = layerType;

plat_layer_texts.compositeType = shapeType;

plat_layer_texts.composite_gfsid = shapegfsID;

plat_layer_texts.shapeType = shapeType_comp;

plat_layer_texts.gfsid = shapegfsID_comp;

plat_layer_texts.text = textValue;

plat_layer_texts.justification = textJustification;

plat_layer_texts.angle = textAngle;

plat_layer_texts.width = textWidth;

plat_layer_texts.height = textHeight;

plat_layer_texts.coordinates = coordinates;

plat_layer_texts.keyValue = keyValue;

plat_Main.Add(plat_layer_texts);

if (coordinates != null)

{

coordinates.Clear();

}

if (plat_layer_texts != null)

{

plat_layer_texts = null;

}

}

}

#endregion

}

if (shapeType.ToUpper().Trim() == "POLYLINE")

{

#region Its Just Polyline

string textValue = string.Empty;

string textJustification = string.Empty;

double textAngle = 0;

double textHeight = 0;

double textWidth = 0;

XmlNodeList xnl3 = node2.SelectNodes("Point");

string keyValue = string.Empty;

foreach (XmlNode node4 in xnl3)

{

string y = node4.Attributes["y"].InnerText;

string x = node4.Attributes["x"].InnerText;

string coordinate = x + "," + y;

coordinates.Add(coordinate);

}

try

{

XmlNodeList xnl4 = node2.SelectNodes("Key");

foreach (XmlNode node5 in xnl4)

{

keyValue = node5.Attributes["value"].InnerText;

break;

}

}

catch

{

}

//finally creating the Point Object for Number of Type = Text

plat_layer_texts = new Plat();

plat_layer_texts.layerName = layerName;

plat_layer_texts.layerType = layerType;

plat_layer_texts.compositeType = "";

plat_layer_texts.composite_gfsid = "";

plat_layer_texts.shapeType = shapeType;

plat_layer_texts.gfsid = shapegfsID;

plat_layer_texts.text = textValue;

plat_layer_texts.justification = textJustification;

plat_layer_texts.angle = textAngle;

plat_layer_texts.width = textWidth;

plat_layer_texts.height = textHeight;

plat_layer_texts.coordinates = coordinates;

plat_layer_texts.keyValue = keyValue;

plat_Main.Add(plat_layer_texts);

if (coordinates != null)

{

coordinates.Clear();

}

if (plat_layer_texts != null)

{

plat_layer_texts = null;

}

#endregion

}

}

#endregion

}

}

}

}

catch

{

}

finally

{

}

return plat_Main;

}

#endregion

}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Header.cs

-------------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ReadAndImportGARfile.BO

{

public class Header

{

//Groups Tag information

public string fileName = string.Empty;

//Header Tag information

public string geostationVersion = string.Empty;

public string dateModified = string.Empty;

public string dateCreated = string.Empty;

public string userValue = string.Empty;

public string ext_MinY = string.Empty;

public string ext_MinX = string.Empty;

public string ext_MaxY = string.Empty;

public string ext_MaxX = string.Empty;

//Coordinate Tag information

public string projection = string.Empty;

public double scale = 0;

public string units = string.Empty;

//AOI Tag information

public string surveySystemType = string.Empty;

//DLS

public string township = string.Empty;

public string range = string.Empty;

public string meridianDir = string.Empty;

public string meridian = string.Empty;

//NTS

public string coor_unit = string.Empty;

public string primary = string.Empty;

public string sheet = string.Empty;

public string letter = string.Empty;

public string block = string.Empty;

}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Plat.cs

---------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ReadAndImportGARfile.BO

{

public class Plat

{

//Layer details

public string layerName = string.Empty;

public string layerType = string.Empty;

//Text / symbole and its Properties from composite group

public string compositeType = string.Empty;

public string composite_gfsid = string.Empty;

public string shapeType = string.Empty;

public string gfsid = string.Empty;

public string text = string.Empty;

public string justification = string.Empty;

public double angle = 0;

public double width = 0;

public double height = 0;

public IList<string> coordinates = new List<string>();

public string keyValue = string.Empty;

}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Outcomes