在xml中查找字符串并获取相应的姐妹(?)节点

时间:2017-12-26 14:35:21

标签: c# xml

我正在创建一个读取文件cvc的工具,并在xml中搜索cvc值。它有多个称为发布的父笔记。我已经设法让它找到xml中的cvc值,但我无法做到,它得到了相应的姐妹(对不起,我不知道它的叫声)节点。 这是XML的一部分:

<releases>
    <release>
        <id>1</id>
        <name>name1</name>
        <publisher></publisher>
        <region>WLD</region>
        <languages>en,fr,de,it,es,ru,ja</languages>
        <group></group>
        <imagesize>16</imagesize>
        <serial>LA-H-AAAAA</serial>
        <titleid>01007EF00011E000 </titleid>
        <imgcrc>5DD119C1</imgcrc>
        <filename>test</filename>
        <releasename>test</releasename>
        <trimmedsize>0</trimmedsize>
        <firmware>1.0.0</firmware>
        <type>1</type>
        <card>1</card>
    </release>
    <release>
        <id>2</id>
        <name>name2</name>
        <publisher></publisher>
        <region>WLD</region>
        <languages>en,fr,de,it,es,nl,pt,ru,ja</languages>
        <group></group>
        <imagesize>8</imagesize>
        <serial>LA-H-AABPA</serial>
        <titleid>0100152000022000</titleid>
        <imgcrc>1912A1DF</imgcrc>
        <filename>test</filename>
        <releasename>test</releasename>
        <trimmedsize>0</trimmedsize>
        <firmware>1.0.0</firmware>
        <type>1</type>
        <card>1</card>
    </release>

这是我的代码:

XmlNode node = doc.SelectSingleNode ("releases/*[contains(name(),'release')]/imgcrc[text() = '" + textBox6.Text + "']");
if (node == null)
{
    XmlNode id = node.ParentNode.SelectSingleNode ("titleid");
    XmlNode serial = node.ParentNode.SelectSingleNode ("serial");
    XmlNode r = node.ParentNode.SelectSingleNode ("region");
    XmlNode fw = node.ParentNode.SelectSingleNode ("firmware");

    textBox8.Text = id.InnerText;
    textBox4.Text = r.InnerText;
    if (textBox4.Text == "WLD") {
        textBox4.Text = "Worldwide";
    }
    textBox3.Text = fw.InnerText;
    if (textBox3.Text == "") {
        textBox3.Text = "The required FW wasn't found yet";
    }
    textBox2.Text = serial.InnerText;
} else {
    textBox2.Text = "Game is not in the DB or you modified it";
    textBox3.Text = "Game is not in the DB or you modified it";
    textBox4.Text = "Game is not in the DB or you modified it";
    textBox8.Text = "Game is not in the DB or you modified it";
}

很抱歉,如果我的问题不够清楚,或者格式错误。

2 个答案:

答案 0 :(得分:0)

如果你使用这样的路径,你将获得release元素,而不仅仅是imgcrc元素:

var path = "releases/release[imgcrc = '"+ crc +"']";
var node = doc.SelectSingleNode(path);

现在您可以从那里获取相关的子值:

if(node != null)
{
    var titleId = node["titleid"].InnerText;
    var serial = node["serial"].InnerText;
    var region = node["region"].InnerText;
    var firmware = node["firmware"].InnerText;

    //etc
}

答案 1 :(得分:0)

试试xml linq:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("release").Select(x => new {
                        id = (int)x.Element("id"),
                        name = (string)x.Element("name"),
                        publisher = (string)x.Element("publisher"),
                        region = (string)x.Element("region"),
                        languages = (string)x.Element("languages"),
                        group = (string)x.Element("group"),
                        imagesize = (int)x.Element("imagesize"),
                        serial = (string)x.Element("serial"),
                        titleid = (string)x.Element("titleid"),
                        imgcrc = (string)x.Element("imgcrc"),
                        filename = (string)x.Element("filename"),
                        releasename= (string)x.Element("releasename"),
                        trimmedsize = (int)x.Element("trimmedsize"),
                        firmware = (string)x.Element("firmware"),
                        type = (int)x.Element("type"),
                        card = (int)x.Element("card")
            }).ToList();

        }
    }
}