XML文件探索和值执行

时间:2016-03-13 12:15:11

标签: c# xml xmldocument

    <imgdir name="02380000">
    <imgdir name="info">
        <canvas name="icon" width="32" height="32">
            <vector name="origin" x="0" y="32"/>
        </canvas>
        <canvas name="iconRaw" width="27" height="38">
            <vector name="origin" x="0" y="38"/>
        </canvas>
        <int name="price" value="1"/>
        <int name="tradeBlock" value="1"/>
        <int name="bigSize" value="1"/>
        <int name="only" value="1"/>
        <int name="monsterBook" value="1"/>
        <int name="mob" value="100100"/>
    </imgdir>
    <imgdir name="spec">
        <int name="consumeOnPickup" value="1"/>
    </imgdir>
</imgdir>

大家好,我试图只获得2个特定的数据行但不幸的是它没有用,因为我想要得到一些帮助; 3

这是我的代码

 public void ReadValue_FromXML()
    {
        //====================================
        string Path = FilePathString_box.Text;
        Path.Replace(@"\", @"\\");
        //====================================
        string ConnectionStringPath = "";
        MySqlConnection MySqlCon = new MySqlConnection();
        MySqlCon.ConnectionString = ConnectionStringPath;
        string Update_MonsterCardsData = " INSERT INTO monstercarddata (id,cardid,mobid)VALUES(@Rowid,@CardNumber,@mob);";

        XmlDocument doc = new XmlDocument();
        doc.Load(Path);
        int CountElements;


        var elements = doc.SelectNodes("//imgdir");

        foreach (XmlNode node in elements)
        {

            string MonsterCardIDx;
            string MobID;
            if (ReadNameAndValue(node, out MobID))
            {

                MonsterCardIDx = node.SelectSingleNode("./@name").InnerText;
                if (MonsterCardIDx != "0238.img")
                {
                    if (MobID != "" )
                    {

                        int Row = 1;
                        ElementCount_lebel.Text = Row.ToString();
                        int MonsterCard = Int32.Parse(MonsterCardIDx);
                        MySqlCommand MySqlComnd = new MySqlCommand(Update_MonsterCardsData, MySqlCon);
                        MySqlParameter MonsterCardID = new MySqlParameter("@CardNumber", MySqlDbType.Int32); 
                        MonsterCardID.Value = MonsterCard;
                        MySqlComnd.Parameters.Add(MonsterCardID);

                        MySqlParameter MonsterID = new MySqlParameter("@mob", MySqlDbType.Int32); 
                        MonsterID.Value = MobID;
                        MySqlComnd.Parameters.Add(MonsterID);
                        MySqlParameter RowID = new MySqlParameter("@Rowid", MySqlDbType.Int32); 
                        RowID.Value = Row;
                        MySqlComnd.Parameters.Add(RowID);

                        try
                        {
                            MySqlCon.Open();
                            MySqlComnd.ExecuteNonQuery();
                            MySqlCon.Close();
                            Row++;


                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                }
            }

        }

    }

    private bool ReadNameAndValue(XmlNode parent, out string MobID)
    {

        MobID = "";

        var node = parent.SelectSingleNode("./imgdir");
        if (node == null)
        {
            return false;
        }

        try
        {
            if (parent.SelectSingleNode("./int/name").InnerText == "mob")
            {
                MobID = parent.SelectSingleNode("./int/value").InnerText;
            }

        }
        catch (Exception)
        {
        }

        return true;
    }

我得到的输出是MonasterCard =02380000 and MobID=""

mobID应为100100

3 个答案:

答案 0 :(得分:0)

更新:

目前,您的代码仅检查name第一个孩子<int> 元素的parent属性,这就是为什么它找不到目标{{ 1}}。我建议使用XPath通过检查其<int name="mob" value="100100"/>属性值来查找目标<int>,如下所示:

name

初步回答:

您忘记使用XPath表达式将属性名称前的var mobAttribute = parent.SelectSingleNode("./int[@name = 'mob']/@value"); if(mob != null) { MobID = mobAttribute.Value; } 添加到引用属性:

@

如果没有if (parent.SelectSingleNode("./int/@name").InnerText == "mob") { MobID = parent.SelectSingleNode("./int/@value").InnerText; } 符号,表达式仍然有效,但会引用子元素@,例如,而不是属性<name>。没有这样的元素,这就是name空的原因。

答案 1 :(得分:0)

请在try块中的ReadNameAndValue方法中替换您的代码

var intNodes = node.SelectNodes("./int");
                foreach (var intNode in intNodes)
                {
                    if (((XmlElement) intNode).Attributes["name"].Value == "mob")
                    {
                        MobID = ((XmlElement) intNode).Attributes["value"].Value;
                    }
                }

在您的情况下,您需要读取属性,但不需要读取节点内的文本

答案 2 :(得分:0)

使用Linq。你有一个xml文件,其中有多个标签&#34; int&#34;而你只想要一个名字=暴徒的人。使用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
    {
        static void Main(string[] args)
        {

            string xml =
                    "<imgdir name=\"02380000\">" +
                        "<imgdir name=\"info\">" +
                            "<canvas name=\"icon\" width=\"32\" height=\"32\">" +
                                "<vector name=\"origin\" x=\"0\" y=\"32\"/>" +
                            "</canvas>" +
                            "<canvas name=\"iconRaw\" width=\"27\" height=\"38\">" +
                                "<vector name=\"origin\" x=\"0\" y=\"38\"/>" +
                            "</canvas>" +
                            "<int name=\"price\" value=\"1\"/>" +
                            "<int name=\"tradeBlock\" value=\"1\"/>" +
                            "<int name=\"bigSize\" value=\"1\"/>" +
                            "<int name=\"only\" value=\"1\"/>" +
                            "<int name=\"monsterBook\" value=\"1\"/>" +
                            "<int name=\"mob\" value=\"100100\"/>" +
                        "</imgdir>" +
                        "<imgdir name=\"spec\">" +
                            "<int name=\"consumeOnPickup\" value=\"1\"/>" +
                        "</imgdir>" +
                    "</imgdir>";

            XDocument doc = XDocument.Parse(xml);

            foreach (XElement imgdir in doc.Elements("imgdir"))
            {
                string MobID = (string)imgdir.Descendants("int").ToList().Where(x => (string)x.Attribute("name") == "mob").FirstOrDefault().Attribute("value");
            }
        }
    }
}