且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用LINQ to XML读取XML

更新时间:2022-11-27 09:46:17

请试试这个。



Please try this.

System.Xml.Linq.XDocument objDoc = System.Xml.Linq.XDocument.Load(HttpContext.Server.MapPath("XMLFile.xml"));

var objCases = (from C in objDoc.Descendants("Case") 
                where C.Attribute("Compartment").Value == "1"
                select C.Attribute("Id").Value).ToList();

            foreach (var Ids in objCases)
            {
                 Console.WriteLine(Ids);
            }





如果你想选择所有Case Node然后你可以使用这个





And if you want to select all Case Node then you can use this

var objCases = (from C in objDoc.Descendants("Case")
                     where C.Attribute("Compartment").Value == "1"
                     select C).ToList();