从节点列表XML Xpath C#获取子节点的值 [英] Get the value of a child node from a list of nodes, XML Xpath C#

查看:71
本文介绍了从节点列表XML Xpath C#获取子节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Unity在3D环境中生成节点图的应用程序.为了生成节点,我需要从Graphml文件分配它们的值.

I am working on an app that generates Node graphs in a 3D environment using Unity. In order to generate the Nodes, I need to assign them values from a Graphml file.

我对XML没有太多的经验,所以我不确定任何工作原理.我已经做了一些研究,尝试了很多事情,但是我无法使它起作用.

I dont have much experience with XML, so I'm not sure how anything works. I've done some research and tried many things but I cannot make it work.

这是问题所在.我想访问每个Node/Edge元素的子代的值.但是我不知道如何.这是我的代码.节点和边元素位于xmlNodeLists内部.

This is the problem. I want to access the value of the children of each Node/Edge elements. But I don't know how. This is the code I have. The nodes and edges elements are inside xmlNodeLists.

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Topology;

public class GraphMLReaderThree : MonoBehaviour {

public TextAsset xmlRawFile;

void Start()
{
    string data = xmlRawFile.text;

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(data);

    int scale = 2;

    XmlNodeList nodeList = xmlDoc.GetElementsByTagName("node");
    XmlNodeList edgeList = xmlDoc.GetElementsByTagName("edge");
    Debug.Log(nodeList.Count);
    XmlNode targetNode = 
    nodeList[0].SelectNodes("//data[@key='author.activity']")[0];
    if (targetNode != null)
    {
        string valor = targetNode.InnerText;
        Debug.Log(valor);
    }
    else
    {
        Debug.Log("No nodes found");
    }
} 

这是Graphml的一个节点.我不知道如何使用数据密钥访问那些孩子.

And this is one node of the Graphml. I dont know how to access those Children with the data key thing.

    <node id="247983">
        <data key="author.regDate">2010-10-27 23:09:40</data>
        <data key="author.location">Raleigh, NC</data>
        <data key="author.descr">The official twitter account for Red Hat 
        Training, Certification, and Consulting</data>
        <data key="author.pub.descr">Twitter</data>
        <data key="author.country">us</data>
        <data key="author.website">http://www.redhat.com/services</data>
        <data key="author.friends">813</data>
        <data key="author.activity">1</data>
        <data key="author.origId">208736299</data>
        <data key="author.personName">Red Hat Services</data>
        <data key="author.pub.id">11</data>
        <data key="author.tzone">Central Time (US &amp; Canada)</data>
        <data key="author.lon">-78.6386</data>
        <data key="author.name">RedHat_Services</data>
        <data key="author.lat">35.7721</data>
        <data key="author.audience">18155</data>
        <data key="author.eigen.centrality">1.0</data>
        <data key="author.modularity.class">247983</data>
        <data key="author.modularity.size">15</data>
        <data key="coords.x">7.624482</data>
        <data key="coords.y">-11.719869</data>
        <data key="coords.z">-0.9229746</data>
    </node>

问题是,Debug.Log跳到未找到节点".但是列表中充满了节点元素,并且xPath查询是正确的.也许我没有使用正确的功能.我希望特别是获得此子项的值1,尤其是"author.Activity"子项.并且一旦起作用,请获取所有子代并将其分配给3D节点值.

The problem is, the Debug.Log skips to "No Nodes Found". But the Lists are full of node elements and the xPath query is correct. Maybe im not using the correct functions. I expect to get the value 1 of this child in particular "author.Activity" child in particular. And once it works, get all children and assign them to the 3D Node values.

推荐答案

我使用XML Linq创建了一个嵌套字典:

I used XML Linq to create a nested dictionary :

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

namespace ConsoleApplication100
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            Dictionary<string, Dictionary<string, string>> dict = doc.Descendants("node")
                .GroupBy(x => (string)x.Attribute("id"), y => y.Elements("data")
                    .GroupBy(a => (string)a.Attribute("key"), b => (string)b)
                    .ToDictionary(a => a.Key, b => b.FirstOrDefault()))
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
}

这篇关于从节点列表XML Xpath C#获取子节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆