Linq to XML 查询

发布于 2024-11-07 15:20:04 字数 771 浏览 0 评论 0原文

假设我有一个如下所示的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<Customers>
  <Customer Name="Jason Voorhees" WeaponPurchased="Machette" SalePrice="499.90" />
  <Customer Name="Michael Myers" WeaponPurchased="Kitchen Knife" SalePrice="96.75" />
</Customers>

Is it possible, with Linq, to​​ do things like this:?

foreach customer in Customers select WeaponPurchased where Name equals "Jason Voorhees"

或者:

foreach customer in Customers select customer
label1.Text += "Name: " + customer.Name + Environment.NewLine + "WeaponPurchased: " + customer.WeaponPurchased;

我以前在 MSDN 上见过这种类型的查询,但我收藏夹中的链接现在指向错误的页面,我仍在尝试查找这些特定示例。非常感谢任何帮助,

谢谢

Let's just say I have an XML file that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Customers>
  <Customer Name="Jason Voorhees" WeaponPurchased="Machette" SalePrice="499.90" />
  <Customer Name="Michael Myers" WeaponPurchased="Kitchen Knife" SalePrice="96.75" />
</Customers>

Is it possible, with Linq, to do something like this:?

foreach customer in Customers select WeaponPurchased where Name equals "Jason Voorhees"

or:

foreach customer in Customers select customer
label1.Text += "Name: " + customer.Name + Environment.NewLine + "WeaponPurchased: " + customer.WeaponPurchased;

I've seen this type of query before on MSDN, but the links in my favorites lead to the wrong page now, and I'm still trying to find these particular examples. Any help is much appreciated,

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海少女心 2024-11-14 15:20:04

试试这个:

var doc = XDocument.Load(Path.Combine(path, "file.xml"));
var query = from c in doc.Descendants("Customer")
            where c.Attributes("Name").Single().Value == "Jason Voorhees"
            select c.Attributes("WeaponPurchased").Single().Value;

它将返回带有武器名称的 IEnumerable

Try this:

var doc = XDocument.Load(Path.Combine(path, "file.xml"));
var query = from c in doc.Descendants("Customer")
            where c.Attributes("Name").Single().Value == "Jason Voorhees"
            select c.Attributes("WeaponPurchased").Single().Value;

It will return IEnumerable<string> with names of weapons.

メ斷腸人バ 2024-11-14 15:20:04

尝试使用此查询

XElement root = XDocument.Load(path).Root;
var result = root.Elements("Customers").
             Where(x => x.Attribute("Name").Value =="Jason Voorhees").
             Select(x => x.Attribute("WeaponPurchased").Value));

或者您可以尝试创建在 xml 中定义的类并反序列化它们。

Try to use this query

XElement root = XDocument.Load(path).Root;
var result = root.Elements("Customers").
             Where(x => x.Attribute("Name").Value =="Jason Voorhees").
             Select(x => x.Attribute("WeaponPurchased").Value));

Or you can try to create classes which a defined in xml and deserialize them.

清欢 2024-11-14 15:20:04

尝试这个

 public class Customer
    {
        public string Name { get; set; }
        public string WeaponPurchased { get; set; }
        public string SalePrice { get; set; }
    }

并查询 xml,如下所示

 var customer = from c in XDocument.Load("XMLPATH").Descendants("Customer")
                           where (string)c.Attribute("Name")=="Jason Voorhees"
                           select new Customer
                           {
                               Name=(string)c.Attribute("Name"),
                               WeaponPurchased = (string)c.Attribute("WeaponPurchased"),
                               SalePrice = (string)c.Attribute("SalePrice"),

                           };

            foreach (var item in customer)
            {
                Console.WriteLine(item.WeaponPurchased);

            }

try this

 public class Customer
    {
        public string Name { get; set; }
        public string WeaponPurchased { get; set; }
        public string SalePrice { get; set; }
    }

and query the xml like below

 var customer = from c in XDocument.Load("XMLPATH").Descendants("Customer")
                           where (string)c.Attribute("Name")=="Jason Voorhees"
                           select new Customer
                           {
                               Name=(string)c.Attribute("Name"),
                               WeaponPurchased = (string)c.Attribute("WeaponPurchased"),
                               SalePrice = (string)c.Attribute("SalePrice"),

                           };

            foreach (var item in customer)
            {
                Console.WriteLine(item.WeaponPurchased);

            }
鲜肉鲜肉永远不皱 2024-11-14 15:20:04

不完全是。

您想要查找属性具有特定值的客户,然后选择第二个属性。

更像这样的东西:

from customer in Customers
where customer.Attribute("Name").Value equals "Jason Voorhees"
select customer.Attribute("WeaponPurchased").Value

Not quite.

You want to find a Customer where an attribute has a certain value, and then select a second attribute.

Something more like this:

from customer in Customers
where customer.Attribute("Name").Value equals "Jason Voorhees"
select customer.Attribute("WeaponPurchased").Value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文