(De)Seralise根节点元素和不同的子元素

时间:2017-04-19 11:19:29

标签: javascript c# asp.net

我有不同类型的xml文件,下面是一个例子 Xml标签是动态形成的,我需要根据层次结构从xml文件中获取根标签名称和子标签名称

<Employee>
<PersonalInfo>
  <Id>206</Id>
  <Name>Govinda</Name>
  <Skills>C#,ASP.NET,MVC4,HTML,CSS</Skills>
</PersonalInfo>
<Projects>
  <Working>
    <Name>Project306</Name>
    <Team>Team10</Team>
    <Role>Developer</Role>
  </Working>
  <Completed>
    <Name>Agro Seeds</Name>
    <Team>Team9</Team>
    <Role>Developer</Role>
  </Completed>
</Projects>

提前致谢

1 个答案:

答案 0 :(得分:0)

 XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("~/employee.xml"));
        XmlElement root = xmlDoc.DocumentElement;       
        string Content = string.Empty;           
        Content += "<div>" + root.Name;
        Content += "<ul>";
        foreach (XmlNode node in root)
        {
            Content += node.Name;
            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                Content += "<li>" + (node.ChildNodes[i]).Name + " : " + (node.ChildNodes[i]).InnerText + "</li>";
            }

        }
        Content += "</ul>";
        Content += "</div>";