简体   繁体   English

如何从XML中的属性检索值?

[英]How to retrieve value from attribute in XML?

What I have: 我有的:

<?xml version="1.0" encoding="Unicode" standalone="yes"?>
<FiberItems>
    <Fiber China="1a" England="1b"  Japan="1c"  France="1d" Korea="1e"/>
    <Fiber China="2a" England="2b"  Japan="2c"  France="2d" Korea="2e"/>
</FiberItems>

What I want: 我想要的是:
1.retrive all the value of "China" into a string array. 1.将“ China”的所有值检索到一个字符串数组中。
2.if a value of China is "1a", retrive all the value of the rest attributes(1b,1c,1d,1e),. 2.如果中国的值为“ 1a”,则检索其余属性(1b,1c,1d,1e)的所有值。

What I do: 我所做的:
1. I write codes for purpose 1 , but never works >_< 1.我为目的1编写代码,但是> _ <

        XDocument doc = XDocument.Load("/FiberItems.xml");
        IEnumerable<string> query = from fiber in doc.Root.Elements("Fiber")
                                    select (string)fiber.Attribute("China");

        string[] myString = new string[3];
        for (int i = 0; i < 3; i++)
        {
            foreach (string item in query)
            {
                myString[i] = (string)item;
            }
        }

2. for purpose 2, have no idea yet >_< 2.出于目的2,尚不知道> _ <

need helps 需要帮助

So your requirement is if the attribute China value is "1a" then get all the attribute value of that node. 因此,您的要求是,如果属性China值为“ 1a”,则获取该节点的所有属性值。 I think it should work 我认为应该可以

XDocument doc = XDocument.Load("/FiberItems.xml");
IEnumerable<string> query = from fiber in doc.Root.Elements("Fiber")
                            //condition 1
                            where (string)fiber.Attribute("China") == "1a"
                            //condition 2 : Select all except china attribute
                            select fiber.Attributes().Where(a => a.Name !="China");

You can use the following code: 您可以使用以下代码:

        XDocument root = XDocument.Load("/FiberItems.xml");
        var attributesChina = root.Elements("FiberItems").Elements("Fiber").Attributes("China");
        // attributesChina will contain all the values of china
        foreach (XAttribute china in attributesChina)
        {
            string value = china.value;
        }

You probably shouldn't use an array to collect your data but when you do: 您可能不应该使用数组来收集数据,但是当您这样做时:

    string[] myString = new string[3];
  //  for (int i = 0; i < 3; i++)
  //  {
        int i = 0;
        foreach (string item in query)
        {
            myString[i] = (string)item;
            i += 1;
        }
  //  }

You are doing 3x3 rounds where only 3 are needed. 您正在进行3x3轮,仅需要3轮。

//Load XML element
XElement root = XElement.Parse(File.ReadAllText("/FiberItems.xml")); 

//All china attributes (retrieves 1a,2a)
var chinaAttributes= root.Elements().Attributes("China");

//load all rest attributes for china = 1a, loads 1b,1c,1d,1e
var chinaOneARestAttributes = root.Elements().Where(a=>a.Attribute("China")!=null && a.Attribute("China").Value=="1a").Attributes().Select(x=>x.Value).Where(x=>!String.Equals(x,"1a"));

UPDATED for Null Reference Exception. 空引用异常已更新。 With the data i had tried earlier, i ensured Attribute China was present for all elements. 利用我之前尝试的数据,我确保所有元素都存在Attribute China。

Using LINQ to XML: 使用LINQ to XML:

var xmlStr = @"<?xml version=""1.0"" encoding=""Unicode"" standalone=""yes""?>
<FiberItems>
    <Fiber China=""1a"" England=""1b""  Japan=""1c""  France=""1d"" Korea=""1e""/>
    <Fiber China=""2a"" England=""2b""  Japan=""2c""  France=""2d"" Korea=""2e""/>
</FiberItems>";
var doc = XDocument.Parse(xmlStr);
var query =
    from fiber in doc.Descendants("Fiber")
    where (string)fiber.Attribute("China") == "1a"
    select String.Join(", ",
        (from attr in fiber.Attributes()
         where attr.Name != "China"
         select (string)attr).ToArray());

This will return a sequence of the other attribute values for each Fiber element that contains a China attribute with the value 1a . 这将为每个包含China属性值为1a Fiber元素返回一系列其他属性值。

Check out System.Xml.Linq. 签出System.Xml.Linq。

Here's an example to get a list of all the attributes for one element. 这是获取一个元素的所有属性的列表的示例。

var xml = @"<?xml version=""1.0"" encoding=""Unicode"" standalone=""yes""?>
<FiberItems>
<Fiber China=""1a"" England=""1b""  Japan=""1c""  France=""1d"" Korea=""1e""/>
<Fiber China=""2a"" England=""2b""  Japan=""2c""  France=""2d"" Korea=""2e""/>
</FiberItems>";

XDocument doc = XDocument.Parse(xml);
XElement ele = doc.Root.Element("Fiber");
var att = ele.Attributes();

In XPath: 在XPath中:

/FiberItems/Fiber[@China='1a']/@*

gets you all the attributes of Fiber elements with China='1a' and 使用China ='1a'获得纤维元素的所有属性,然后

/FiberItems/Fiber[@China='1a']/@*[local-name()!='China']

gets you the same sequence of attributes excluding the China attribute. 为您提供相同的属性序列(不包括China属性)。

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

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