简体   繁体   中英

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.
2.if a value of China is "1a", retrive all the value of the rest attributes(1b,1c,1d,1e),.

What I do:
1. I write codes for purpose 1 , but never works >_<

        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 >_<

need helps

So your requirement is if the attribute China value is "1a" then get all the attribute value of that node. 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.

//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.

Using 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 .

Check out 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:

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

gets you all the attributes of Fiber elements with China='1a' and

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

gets you the same sequence of attributes excluding the China attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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