C#中的LINQ to XML - 的XDocument [英] c# LINQ to XML - XDocument

查看:375
本文介绍了C#中的LINQ to XML - 的XDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想阅读使用的XDocument 类简单的XML文档。
上的文档阅读,但我挣扎映射我的 OperationConfig 的XML?

Just trying to read a simple XML doc in using XDocument class. The document is read, but I'm struggling mapping my OperationConfig to the XML?

var xml = XDocument.Load(path);
var query = xml.Root.Elements("configaccount")
    .Select(o => new OperationConfig()
    {
        AccountName = o.Attribute("accountname").Value,
        Email = o.Attribute("email").Value
    });



中的XML:

The XML:

<?xml version="1.0" encoding="UTF-8"?>
    <config>
        <configaccount>
            <accountname>
                BusinessName
            </accountname>
            <email>
                aa@domain.com
            </email>
        </configaccount>
    </Config>



不知道我已经错过了因为我重新调整空?

Not sure what I've missed as I'm retuning null?

推荐答案

您需要通过名字从XML以检索后代节点。同时ACCOUNTNAME和电子邮件不是一个属性,它的XML的元素。属性是内部因素。

You need to retrive Descendants node by name from xml. Also "accountname" and "email" is not an attributes, its an Element of XML. Attributes are inside element.

替换您的查询

 var query = xml.Descendants("configaccount")
        .Select(o => new OperationConfig
        {
            AccountName = o.Element("accountname").Value,
            Email = o.Element("email").Value
        });



希望这有助于

Hope this help

这篇关于C#中的LINQ to XML - 的XDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆