简体   繁体   English

将xml树转换为json树

[英]Converting xml tree to json tree

Here's my code which succesfully creates XML: 这是我的成功创建XML的代码:

XDocument xdoc = new XDocument();
XElement root = new XElement("tree");
root.Add(new XAttribute("id", 0));
xdoc.Add(root);

new BuildFoldersTree(root, db);

var items = (from x in db.Items orderby x.name select new { x.name, x.id, x.parent }).ToList();
foreach (var p in items)
{
    XElement e = new XElement("item",
        new XAttribute("text", p.name),
        new XAttribute("id", p.id),
        new XAttribute("parentId", p.parent));
    XElement parent = root.XPathSelectElement(String.Format("//*[@id=\"FOLDER_{0}\"] ", p.parent.ToString()));
    if (parent != null) parent.Add(e);
 }

and: 和:

public void BuildFoldersTree(XElement root, MyEntities db)
{
    List<Folder> folders = (from x in db.Folders orderby x.parent select x).ToList();

    for (int i = 0; i < folders.Count; i++)
    {
        int f_id = folders[i].parent;
        Folder folder = folders[i];

        XElement e = new XElement("item",
            new XAttribute("text", folder.name),
            new XAttribute("id", "FOLDER_" + folder.id.ToString()),
            new XAttribute("parentId", folder.parent));

        if (folder.parent == 0)
        {
            root.Add(e);
        }
        else
        {
            XElement parent = root.XPathSelectElement(String.Format("//*[@id=\"FOLDER_{0}\"] ", folder.parent.ToString()));
             parent.Add(e);
        }
    }
}

Here's what's going on there: I have two tables in my database. 这是正在发生的事情:我的数据库中有两个表。 One for Folders, and one for Items. 一个用于文件夹,另一个用于项目。 Each item has a parent folder. 每个项目都有一个父文件夹。 The item has a column 'parent' which is integer and which represents the folder id. 该项目具有一列“父”,该列是整数,代表文件夹ID。 But, each folder also has a parent. 但是,每个文件夹也都有一个父文件夹。 The folder has a column named 'parent' which is integer and which represents an id of another folder. 该文件夹有一个名为“ parent”的列,该列是整数,代表另一个文件夹的ID。

With that code, I'm creating an xml tree with folders, and then I add the items to the correct folder. 使用该代码,我将创建带有文件夹的xml树,然后将项目添加到正确的文件夹中。

The previous code works. 先前的代码有效。

Now, I need to make the same algorithm but for Json. 现在,我需要为Json做相同的算法。 So, it won't use Xml, it should create a Json tree. 因此,它不会使用Xml,而应该创建一个Json树。

I have no idea on how to begin. 我不知道如何开始。 What should I use? 我应该使用什么?

Here's an example of what the result xml looks like: 这是结果xml的示例:

<tree id="0">
    <item text="Folder_name" id="FOLDER_1" parentId="0">
        <item text="Other folder name" id="FOLDER_96" parentId="1">
            <item text="Third folder name" id="FOLDER_127" parentId="96">
                <item text="New folder" id="FOLDER_147" parentId="127" />
                <item text="item name" id="959" parentId="147" />
                <item text="item name sdgdfh" id="1152" parentId="147" />
            </item>
        </item>
    </item>
</tree>

There is functionality in the JSON.NET library for this. JSON.NET库中有此功能。 You can use the SerializeXmlNode method of the JsonConvert class contained in the JSON.NET library. 您可以使用JSON.NET库中包含的JsonConvert类的SerializeXmlNode方法。 Your code would look as follows: 您的代码如下所示:

var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(xmlDocument);

You can find more information here: http://james.newtonking.com/projects/json/help/index.html?topic=html/ConvertingJSONandXML.htm 您可以在此处找到更多信息: http : //james.newtonking.com/projects/json/help/index.html?topic=html/ConvertingJSONandXML.htm

If we apply this to your example, we have the following: 如果将其应用于您的示例,我们将具有以下优势:

string xml = "<tree id=\"0\">" +
                        "<item text=\"Folder_name\" id=\"FOLDER_1\" parentId=\"0\">" +
                            "<item text=\"Other folder name\" id=\"FOLDER_96\" parentId=\"1\">" +
                                "<item text=\"Third folder name\" id=\"FOLDER_127\" parentId=\"96\">" +
                                    "<item text=\"New folder\" id=\"FOLDER_147\" parentId=\"127\" />" +
                                    "<item text=\"item name\" id=\"959\" parentId=\"147\" />" +
                                    "<item text=\"item name sdgdfh\" id=\"1152\" parentId=\"147\" />" +
                                "</item>" +
                            "</item>" +
                        "</item>" +
                    "</tree>";

var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(xmlDocument);

The json variable now contains the following data: json变量现在包含以下数据:

{
  "tree":{
  "@id":"0",
  "item":{
     "@text":"Folder_name",
     "@id":"FOLDER_1",
     "@parentId":"0",
     "item":{
        "@text":"Other folder name",
        "@id":"FOLDER_96",
        "@parentId":"1",
        "item":{
           "@text":"Third folder name",
           "@id":"FOLDER_127",
           "@parentId":"96",
           "item":[
              {
                 "@text":"New folder",
                 "@id":"FOLDER_147",
                 "@parentId":"127"
              },
              {
                 "@text":"item name",
                 "@id":"959",
                 "@parentId":"147"
              },
              {
                 "@text":"item name sdgdfh",
                 "@id":"1152",
                 "@parentId":"147"
              }
           ]
        }
     }
  }
}

This is a JSON representation of your XML document. 这是XML文档的JSON表示形式。

我认为最简单的方法是使用Json.Net并将XDocument序列化为json。

var jsonstr = JsonConvert.SerializeXNode(xdoc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM