简体   繁体   English

找不到商品名称C#XML

[英]Cannot find item name C# XML

I'm having a problem with my XML document. 我的XML文档有问题。

I want my program to find all values of the items in my XML file, but only if the handlingType is of a certain character bunch. 我希望我的程序在XML文件中查找项目的所有值,但前提是该handlingType是某个字符束。

Code (C#) : 代码(C#):

string path = "//files//handling.meta";

             var doc = XDocument.Load(path);

            var items = doc.Descendants("HandlingData").Elements("Item");

            var query = from i in items

                        select new
                         {
                             HandlingName = (string)i.Element("handlingName"),
                             HandlingType = (string)i.Element("HandlingType"),
                             Mass = (decimal?)i.Element("fMass")
                         };
            foreach (var HandlingType in items)
            {
                if (HandlingType.ToString() == "HANDLING_TYPE_FLYING")
                {
                    MessageBox.Show(HandlingType.ToString());
                }
            }

The above code demonstraights a short version of what I want to happen, but fails to find this handlingType (does not show the messageBox) 上面的代码演示了我想发生的事情的简短版本,但是找不到此handlingType(不显示messageBox)

Here's the XML : 这是XML:

<CHandlingDataMgr>
    <HandlingData>
        <Item type="CHandlingData">
            <handlingName>Plane</handlingName>
            <fMass value="380000.000000"/>
            <handlingType>HANDLING_TYPE_FLYING</handlingType>
        </Item>
        <Item type="CHandlingData">
            <handlingName>Car1</handlingName>
            <fMass value="150000.000000"/>
            <handlingType>HANDLING_TYPE_DRIVING</handlingType>
        </Item>
    </HandlingData>
</CHandlingDataMgr>

I would like the output to show the handlingName if it contains a certain HandlingType For eg 我希望输出如果包含某个HandlingType例如,则显示handlingName

if (handlingType == "HANDLING_TYPE_FLYING") 
{
    messageBox.Show(this.HandlingName);
}

My problem in short : Program does not find item's handling type, it does find the tag but when asked to display, returns empty/shows as nothing. 简而言之,我的问题是:程序找不到项目的处理类型,它确实找到了标签,但是当要求显示时,返回空/无显示。

Edit: Also in the XML handling_type_flying contains extra elements such as thrust that cannot be found in each item (such as car), I would like the program to also find these elements. 编辑:此外,在XMLhandling_type_flying中还包含在每个项目(例如汽车)中找不到的额外元素(例如推力),我希望程序也可以找到这些元素。 (this is a second problem I'm facing, maybe should ask 2nd ques?) (这是我面临的第二个问题,也许应该问第二个问题?)

Several things that need fixing. 需要修复的几件事。

  1. you are not using your query in your foreach loop. 您没有在foreach循环中使用查询。 foreach (var item in query)
  2. Your element has an upercase "H" but should be lowercase "handlingType". 您的元素的大小写为“ H”,但应为小写的“ handlingType”。 HandlingType = (string)i.Element("handlingType"),
  3. You are not pulling the Attribute value of your fMass element. 您没有提取fMass元素的Attribute值。 Mass = i.Element("fMass").Attribute("value").Value
  4. Once you adjust your Query in your foreach loop you then need to adjust the loop to account for looping over your newly made object. 在foreach循环中调整查询后,您需要调整循环以解决在新创建的对象上的循环。
  5. NOTE that I removed (decimal) from Mass = i.Element("fMass").Attribute("value").Value 注意,我从Mass = i.Element(“ fMass”)。Attribute(“ value”)。Value中删除(十进制)

here is the code with all the fixes. 这是包含所有修复程序的代码。

  class Program
    {
        static void Main()
        {
            const string path = "//files//handling.meta";
            var doc = XDocument.Load(path);

            var items = doc.Descendants("HandlingData").Elements("Item");

            var query = from i in items

                        select new
                        {
                            HandlingName = (string)i.Element("handlingName"),
                            HandlingType = (string)i.Element("handlingType"),
                            Mass = i.Element("fMass").Attribute("value").Value
                        };
            foreach (var item in query)
            {
                if (item.HandlingType == "HANDLING_TYPE_FLYING")
                {
                    //Remove messagebox if consoleapp
                    MessageBox.Show(item.HandlingType);
                    MessageBox.Show(item.HandlingName);
                    Console.WriteLine(item.HandlingType);
                    Console.WriteLine(item.HandlingName);
                }
            }
        }
    }

I would recommend looking into serializing your xml to an object. 我建议您考虑将xml序列化为一个对象。

If you look at http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=vs.110).aspx the ToString() method doesn't return the name of the tag, but the indented XML. 如果您查看http://msdn.microsoft.com/zh-cn/library/system.xml.linq.xelement(v=vs.110).aspx,则ToString()方法不会返回标记的名称,但缩进XML。

You should instead be using the Value property. 您应该改为使用Value属性。 Also you should use .equals("...") instead of == 另外,您应该使用.equals(“ ...”)而不是==

if (handlingType.Value.equals("HANDLING_TYPE_FLYING")
{
    messageBox.Show(this.handlingname);
}

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

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