简体   繁体   English

C#LINQ转XML-XDocument

[英]c# LINQ to XML - XDocument

Just trying to read a simple XML doc in using XDocument class. 只是尝试使用XDocument类读取简单的XML文档。 The document is read, but I'm struggling mapping my OperationConfig to the XML? 阅读了文档,但是我很难将我的OperationConfig映射到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
    });

The XML: 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? 不知道我要重新调整为null时错过了什么?

You need to retrive Descendants node by name from xml. 您需要按名称从xml检索Descendants节点。 Also "accountname" and "email" is not an attributes, its an Element of XML. 另外,“帐户名”和“电子邮件”不是属性,而是XML的元素。 Attributes are inside element. 属性位于元素内部。

Replace your query 替换您的查询

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

Hope this help 希望这个帮助

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

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