I assign a value to two class fields as follows.

static Dictionary<string, string> XmlToDict(string loadFile) { Dictionary<string, string> dic = new Dictionary<string, string>(); XmlDocument document = new XmlDocument(); document.Load(loadFile); foreach (XmlNode node in document.DocumentElement.ChildNodes) { string value = node.Attributes["value"].Value; string key = node.Name; if (!string.IsNullOrEmpty(value)) { dic.Add(key, value); } } return dic; } static void Main(string[] args) { string rusXml = "D:\\My Documents\\Visual Studio 2010\\Projects\\obyedinenieDannix\\obyedinenieDannix\\rusXml.xml"; string engXml = "D:\\My Documents\\Visual Studio 2010\\Projects\\obyedinenieDannix\\obyedinenieDannix\\engXml.xml"; Dictionary<string, string> dic1 = XmlToDict(rusXml); Dictionary<string, string> dic2 = XmlToDict(engXml); var vivod = dic1.Select( kvp => new ClassVivod() { _key = kvp.Key, _valueXML_1 = kvp.Value }) .ToArray(); 

class itself:

  public class ClassVivod { public string _key; public string _valueXML_1; public string _valueXML_2; public string Key { get { return _key; } set { _key = value; } } public string ValueXML_1 { get { return _valueXML_1; } set { _valueXML_1 = value; } } public string ValueXML_2 { get { return _valueXML_2; } set { _valueXML_2 = value; } } } 

ClassVivod has another field valueXML_2. There you need to write the value from dic2, provided that key = dic2.Key .. if it is not difficult, key = dic2.Key me how.

  • format the code. And at the same time, explain what ClassVivod is , what field is _valueXML_2 , what is dic2, and how does all of this relate to the code above? - DreamChild
  • ClassVivod is a class with three fields. key and two values ​​(_key, _valueXML_1, _valueXML_2 and all of type string). dic1 and dic2 are Dictionary <string, string> read from different XML. In the code above, I assign the _key, _valueXML_1 class fields to the corresponding values ​​from dic1. And in _valueXML_2, you need to write the value from dic2, provided that _key = dic2.Key .. - alirasul
  • @zirrama Well, I hope you understand that it is better to program with code, not words, so bring the text of your classes better, so it will be clearer. And don't forget to format the code, it's very inconvenient to read this jumble - DreamChild
  • Understand. Just deadlines are pressing here and hurry. I get a job I have been given a task like this .. I added a code. - alirasul

1 answer 1

If I understand you correctly, then you need something like this:

 var vivod = dic1.Select(kvp => new ClassVivod { Key = kvp.Key, ValueXML_1 = kvp.Value, ValueXML_2 = dic2.ContainsKey(kvp.Key) ? dic2[kvp.Key] : "" }).ToArray(); 

However, it is possible that you wanted something else.

  • That is necessary) thank you) - alirasul
  • @zirrama is a bit offtopic, but if you get a job and want to provide this code to them, then I would advise you to rewrite it into something better - it is, with one hand on the heart, just terrible. In normal offices, such a code is strongly criticized - DreamChild
  • Thanks for the advice) I usually put everything in order at the end) after I do everything, I'll throw it here for comments. If I look, I will be grateful. - alirasul