Hi,@Noah Aas. Welcome to Microsoft Q&A.
1.Your data values are written in the attribute properties of the tag, and it is best to load the data in a primitive way
public class Parameter
{
public string id { get; set; }
public string name { get; set; }
}
public class Label
{
public string id { get; set; }
public string vendor { get; set; }
public string vendorLocation { get; set; }
}
public class ParametersLabels
{
public string id { get; set; }
public string fKParameter { get; set; }
public string fKLabel { get; set; }
}
public class ParametersLabels_Label_Parameter
{
public string id { get; set; }
public string fKParameter { get; set; }
public string fKLabel { get; set; }
public string vendor { get; set; }
public string vendorLocation { get; set; }
public string name { get; set; }
}
public void GetData(string filePath)
{
//Load XML
XmlDocument dom = new XmlDocument();
dom.Load(filePath);
//Read the data and save it in parameters、labels、parametersLabels
XmlElement root = dom.DocumentElement;
foreach (XmlNode node in root.ChildNodes[0].ChildNodes)
{
parameters.Add(new Parameter() { id = node.Attributes["id"].Value.ToString(), name = node.Attributes["value"].Value.ToString() });
}
foreach (XmlNode node in root.ChildNodes[1])
{
labels.Add(new Label() { id = node.Attributes["id"].Value.ToString(), vendor = node.Attributes["vendor"].Value.ToString(), vendorLocation = node.Attributes["vendorLocation"].Value.ToString() });
}
foreach (XmlNode node in root.ChildNodes[2])
{
parametersLabels.Add(new ParametersLabels() { id = node.Attributes["id"].Value.ToString(), fKParameter = node.Attributes["fKParameter"].Value.ToString(), fKLabel = node.Attributes["fKLabel"].Value.ToString() });
}
}
2. The syntax you described is a Linq expression, please refer to Query expression basics (LINQ in C#) - C# | Microsoft Learn for more information about Linq expressions. It is recommended to use the Linq syntax instead of the Linq method.
The following is the use of the Linq method
public List<string> quert_Linq_method_string(string vendor, string vendorLocation)
{
var query = labels.Join(parametersLabels, label => label.id, parameterLabel => parameterLabel.fKLabel,
(label, parameterLabel) => new { label, parameterLabel })
.Join(parameters, temp1 => temp1.parameterLabel.fKParameter, parameter => parameter.id, (temp1, parameters) => new { temp1.label, parameters, temp1.parameterLabel })
.Where(p => p.label.vendor.Equals(vendor) && p.label.vendorLocation.Equals(vendorLocation))
.Select(p => p.parameters.name);
return query.ToList<string>();
}
public List<ParametersLabels_Label_Parameter> quert_Linq_method(string vendor, string vendorLocation)
{
var query = labels.Join(parametersLabels, label => label.id, parameterLabel => parameterLabel.fKLabel, (label, parameterLabel) => new { label, parameterLabel })
.Join(parameters, temp1 => temp1.parameterLabel.fKParameter, parameter => parameter.id, (temp1, parameters) => new { temp1.label, parameters, temp1.parameterLabel })
.Where(p => p.label.vendor.Equals(vendor) && p.label.vendorLocation.Equals(vendorLocation))
.Select(p =>
new ParametersLabels_Label_Parameter()
{
id = p.parameterLabel.id,
fKParameter = p.parameterLabel.fKParameter,
fKLabel = p.parameterLabel.fKLabel,
name = p.parameters.name,
vendor = p.label.vendor,
vendorLocation = p.label.vendorLocation
}
);
return query.ToList<ParametersLabels_Label_Parameter>();
}
3.The query results are converted and stored using JsonConvert:
public void SaveData(string path, string data)
{
FileInfo fi = new FileInfo(path);
if (!fi.Exists)
{
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(data);
}
}
}
4. The full code of the console program is as follows(Read XML data + Linq multi-table query + JSON save data)
using Newtonsoft.Json;
using System.Net.Http.Json;
using System.Numerics;
using System.Reflection.Metadata;
using System.Text.Json.Serialization;
using System.Xml;
using System.Xml.Linq;
string filePath = Path.Combine(Environment.CurrentDirectory,"XMLFile1.xml")
var pathBase = Environment.CurrentDirectory;
//Get Data
Class1 class1 = new Class1();
class1.GetData(filePath);
//Query
var list = class1.quert_Linq_method("xxx3", "bbb3");
var list_stringData = class1.quert_Linq_method_string("xxx3", "bbb3");
//Convert to Json
string json = JsonConvert.SerializeObject(list, Newtonsoft.Json.Formatting.Indented);
string json_stringData = JsonConvert.SerializeObject(list_stringData, Newtonsoft.Json.Formatting.Indented);
//Save Data
class1.SaveData(Path.Combine(pathBase, "myText1.txt"),json);
class1.SaveData(Path.Combine(pathBase, "myText2.txt"),json_stringData);
public class Class1
{
public List<Parameter> parameters = new List<Parameter>();
public List<Label> labels = new List<Label>();
public List<ParametersLabels> parametersLabels = new List<ParametersLabels>();
public void GetData(string filePath)
{
//Load XML
XmlDocument dom = new XmlDocument();
dom.Load(filePath);
//Read the data and save it in parameters、labels、parametersLabels
XmlElement root = dom.DocumentElement;
foreach (XmlNode node in root.ChildNodes[0].ChildNodes)
{
parameters.Add(new Parameter() { id = node.Attributes["id"].Value.ToString(), name = node.Attributes["value"].Value.ToString() });
}
foreach (XmlNode node in root.ChildNodes[1])
{
labels.Add(new Label() { id = node.Attributes["id"].Value.ToString(), vendor = node.Attributes["vendor"].Value.ToString(), vendorLocation = node.Attributes["vendorLocation"].Value.ToString() });
}
foreach (XmlNode node in root.ChildNodes[2])
{
parametersLabels.Add(new ParametersLabels() { id = node.Attributes["id"].Value.ToString(), fKParameter = node.Attributes["fKParameter"].Value.ToString(), fKLabel = node.Attributes["fKLabel"].Value.ToString() });
}
Console.WriteLine();
}
public void SaveData(string path,string data)
{
FileInfo fi = new FileInfo(path);
if(!fi.Exists)
{
using(StreamWriter sw = fi.CreateText())
{
sw.WriteLine(data);
}
}
}
public List<string> quert_Linq_method_string(string vendor, string vendorLocation)
{
var query = labels.Join(parametersLabels, label => label.id, parameterLabel => parameterLabel.fKLabel,
(label, parameterLabel) => new { label, parameterLabel })
.Join(parameters, temp1 => temp1.parameterLabel.fKParameter, parameter => parameter.id, (temp1, parameters) => new { temp1.label, parameters, temp1.parameterLabel })
.Where(p => p.label.vendor.Equals(vendor) && p.label.vendorLocation.Equals(vendorLocation))
.Select(p => p.parameters.name);
return query.ToList<string>();
}
public List<ParametersLabels_Label_Parameter> quert_Linq_method(string vendor, string vendorLocation)
{
var query = labels.Join(parametersLabels, label => label.id, parameterLabel => parameterLabel.fKLabel,(label, parameterLabel) => new { label, parameterLabel })
.Join(parameters, temp1 => temp1.parameterLabel.fKParameter, parameter => parameter.id, (temp1, parameters) => new { temp1.label, parameters, temp1.parameterLabel })
.Where(p => p.label.vendor.Equals(vendor) && p.label.vendorLocation.Equals(vendorLocation))
.Select(p =>
new ParametersLabels_Label_Parameter()
{
id = p.parameterLabel.id,
fKParameter = p.parameterLabel.fKParameter,
fKLabel = p.parameterLabel.fKLabel,
name = p.parameters.name,
vendor = p.label.vendor,
vendorLocation = p.label.vendorLocation
}
);
return query.ToList<ParametersLabels_Label_Parameter>();
}
}
public class Parameter
{
public string id { get; set; }
public string name { get; set; }
}
public class Label
{
public string id { get; set; }
public string vendor { get; set; }
public string vendorLocation { get; set; }
}
public class ParametersLabels
{
public string id { get; set; }
public string fKParameter { get; set; }
public string fKLabel { get; set; }
}
public class ParametersLabels_Label_Parameter
{
public string id { get; set; }
public string fKParameter { get; set; }
public string fKLabel { get; set; }
public string vendor { get; set; }
public string vendorLocation { get; set; }
public string name { get; set; }
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.