Run Code
|
Code Wall
|
Users
|
Misc
|
Feedback
|
About
|
Login
|
Theme
|
Privacy
xml into dictionary
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Xml; using System.Xml.Linq; namespace Rextester { public class Program { public static void Main(string[] args) { string data = "<data><test>foo</test><test>foobbbbb</test><bar>123</bar><username>foobar</username></data>"; XDocument doc = XDocument.Parse(data); Dictionary<string, string> dataDictionary = new Dictionary<string, string>(); foreach (XElement element in doc.Descendants().Where(p => p.HasElements == false)) { int keyInt = 0; string keyName = element.Name.LocalName; var parent = element.Parent; while(parent != null) { keyName = parent.Name.LocalName + "." + keyName; parent = parent.Parent; } while (dataDictionary.ContainsKey(keyName)) { keyName = keyName + "_" + keyInt++; } dataDictionary.Add(keyName, element.Value); } foreach(var x in dataDictionary) { Console.WriteLine("keyName: " + x.Key + " value: " + x.Value); } } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Easter Holiday Calculator
Date Diff
more or less
dynamic in C#
sss
Random
List<int> into comma delimited string
CommandForce3
Insert m into n
card 2
Please log in to post a comment.