当前位置: 动力学知识库 > 问答 > 编程问答 >

Add,Edit and Delete XML multi level node using c#

问题描述:

This is my XML structure, I need a C# program to add, edit and delete the element with N level.

<root>

<TopLevelMenu ID="&amp;File">

<MenuItem ID="New" OnClick="_New" />

<MenuItem ID="Open" OnClick="_Open" />

<MenuItem ID="-" />

<MenuItem ID="Close" OnClick="_Close" />

<MenuItem ID="-" />

<MenuItem ID="E&amp;xit" OnClick="_Exit" />

</TopLevelMenu>

<TopLevelMenu ID="&amp;Edit">

<MenuItem ID="Undo" OnClick="_Undo" />

<MenuItem ID="-" />

<MenuItem ID="Cut" OnClick="_Cut" />

<MenuItem ID="Copy" OnClick="_Copy" />

<MenuItem ID="Paste" OnClick="_Paste" />

<MenuItem ID="-" />

<MenuItem ID="Options">

<MenuItem ID="Sub Menu Item">

<MenuItem ID="Sub Sub Menu Item" />

</MenuItem>

</MenuItem>

</TopLevelMenu>

</root>

网友答案:

Try this recursive code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication34
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
            "<root>\n" +
              "<TopLevelMenu ID=\"Imports&amp;FileImports\">\n" +
                "<MenuItem ID=\"ImportsNewImports\" OnClick=\"Imports_NewImports\"/>\n" +
                "<MenuItem ID=\"ImportsOpenImports\" OnClick=\"Imports_OpenImports\"/>\n" +
                "<MenuItem ID=\"Imports-Imports\" />\n" +
                "<MenuItem ID=\"ImportsCloseImports\" OnClick=\"Imports_CloseImports\"/>\n" +
                "<MenuItem ID=\"Imports-Imports\"/>\n" +
                "<MenuItem ID=\"ImportsE&amp;xitImports\" OnClick=\"Imports_ExitImports\"/>\n" +
              "</TopLevelMenu\n>" +
              "<TopLevelMenu ID=\"Imports&amp;EditImports\">\n" +
                "<MenuItem ID=\"ImportsUndoImports\" OnClick=\"Imports_UndoImports\"/>\n" +
                "<MenuItem ID=\"Imports-Imports\"/>\n" +
                "<MenuItem ID=\"ImportsCutImports\" OnClick=\"Imports_CutImports\"/>\n" +
                "<MenuItem ID=\"ImportsCopyImports\" OnClick=\"Imports_CopyImports\"/>\n" +
                "<MenuItem ID=\"ImportsPasteImports\" OnClick=\"Imports_PasteImports\"/>\n" +
                "<MenuItem ID=\"Imports-Imports\"/>\n" +
                "<MenuItem ID=\"ImportsOptionsImports\">\n" +
                  "<MenuItem ID=\"ImportsSub Menu ItemImports\">\n" +
                    "<MenuItem ID=\"ImportsSub Sub Menu ItemImports\"/>\n" +
                  "</MenuItem>\n" +
                "</MenuItem>\n" +
              "</TopLevelMenu>\n" +
            "</root>\n";

            XDocument doc = XDocument.Parse(input);
            var results = doc.Descendants("TopLevelMenu").Elements("MenuItem").Select(y => GetMenu(y)).ToList();

        }
        static object GetMenu(XElement element)
        {
            if (!element.HasElements)
            {
                return element.Attribute("ID").Value;
            }
            else
            {
                return (new { id = element.Attribute("ID").Value, child = GetMenu(element.Element("MenuItem"))});
            }
        }

    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: