Nested XML to Dictionary

前端 未结 4 1090
既然无缘
既然无缘 2021-01-25 03:44

I am trying to convert an XML data into dictionary. I am having problems with identical node names. C# .Net 3.5

Sample XML = the problem is I have no control over this.

4条回答
  •  情话喂你
    2021-01-25 04:23

    just made this, works for me. bit clunky. use it as a template.

            public static Dictionary RecDis(string ThisXML)
        {
            Dictionary ThisBlock = new Dictionary();
    
            XElement doc = XElement.Parse(ThisXML);
    
            XNode[] ThisNoideArray = doc.Nodes().ToArray();
    
            foreach (XNode park in ThisNoideArray)
            {
                XElement parker = XElement.Parse(park.ToString());
    
                if (parker.HasElements)
                {
                    ThisBlock.Add(parker.Name.ToString(), RecDis(parker.ToString()));
                }
                else
                {
                    ThisBlock.Add(parker.Name.ToString(), parker.Value.ToString());
                }
            }
    
            return ThisBlock;
        }
    

提交回复
热议问题