C#读取XML数据并报存到Dictionary

XMLHelper xML = new XMLHelper();
var dic = xML.GetData("ActualInfoTypes", "TypeName");
string value = string.Empty;
dic.TryGetValue(name, out value);
public Dictionary<string, string> GetData(string parentNodeName, string childNodeValueName)
        {
            string xmlSetting = ConfigurationManager.AppSettings["mappingConfig"];
            var dic = new Dictionary<string, string>();
            
            XDocument doc = XDocument.Load(xmlSetting);
            foreach (XElement element in doc.Element(parentNodeName).Elements())
            {
                dic.Add(element.Attribute("name").Value.ToString(),
                               element.Element(childNodeValueName).Value.ToString());
            }
            return dic;
        }
<?xml version="1.0" encoding="utf-8" ?>
<ActualInfoTypes>
  <ActualInfoType name="">
    <TypeName></TypeName>
  </ActualExecutionInfoType>
 <ActualInfoType name="">
    <TypeName></TypeName>
  </ActualExecutionInfoType>
 <ActualInfoType name="">
    <TypeName></TypeName>
  </ActualExecutionInfoType>
</ActualInfoTypes>

posted @ 2022-10-24 16:28  highlightyys  阅读(44)  评论(0)    收藏  举报