c# - 如何访问子 xml 元素?

标签 c# xml linq linq-to-xml

我在旧数据库中保存了一个 xml 字符串,我正在尝试解析该字符串。我可以获取字符串,但获取我需要的值时遇到两个问题。首先,一些示例 xml。

<?xml version="1.0" encoding="utf-16"?>
<email>
    <meta>
        <smartForm>
            <unit name="ForgotUsername" label="Forgot Username Email">
                <textBox name="FromEmail" label="From Email" type="Email" />
                <textBox name="FromName" label="From Name" type="100" />
                <textBox name="BccEmail" label="BCC" type="EmailList" />
                <textBox name="Subject" label="Subject" type="300" />
                <textBox2 name="TextBody" label="Body" type="Memo" />
            </unit>
            <unit name="ForgotPassword" label="Forgot Password Email">
                <textBox name="FromEmail" label="From Email" type="Email" />
                <textBox name="FromName" label="From Name" type="100" />
                <textBox name="BccEmail" label="BCC" type="EmailList" />
                <textBox name="Subject" label="Subject" type="300" />
                <textBox2 name="TextBody" label="Body" type="Memo" />
            </unit>
        </smartForm>
    </meta>
    <value>&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;root&gt;&lt;ForgotPassword BccEmail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b5f4e585f6b5f4e585f05484446" rel="noreferrer noopener nofollow">[email protected]</a>" FromEmail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfbbaabcbb8fbbaabcbbe1aca0a2" rel="noreferrer noopener nofollow">[email protected]</a>" FromName="password test" Subject="password test" TextBody="info" /&gt;&lt;ForgotUsername BccEmail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4c0d1c7c0f4c0d1c7c09ad7dbd9" rel="noreferrer noopener nofollow">[email protected]</a>" FromEmail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed99889e99ad99889e99c38e8280" rel="noreferrer noopener nofollow">[email protected]</a>" FromName="test" Subject="test" TextBody="test" /&gt;&lt;/root&gt;</value>
</email>

问题 #1 - 我尝试使用 XElement.Parse("string") 解析 xml但是,我无法获取 <value>除非我删除 xml 声明(即前 39 个字符)。我希望不必这样做,因为这是一个脆弱的解决方案。

问题 #2 - 一旦我有了 <value>元素的内容并解析为 XElement,我希望查询 <ForgotUsername><ForgotPassword> <root>的子节点文档元素。当我得到.Elements()时,我被告知该集合为空。

我做错了什么?

替换实体后值的 XML:

<root>
    <ForgotPassword BccEmail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e89c8d9b9ca89c8d9b9cc68b8785" rel="noreferrer noopener nofollow">[email protected]</a>" FromEmail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7a3b2a4a397a3b2a4a3f9b4b8ba" rel="noreferrer noopener nofollow">[email protected]</a>" FromName="password test" Subject="password test" TextBody="info" />
    <ForgotUsername BccEmail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdb9a8beb98db9a8beb9e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>" FromEmail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5a1b0a6a195a1b0a6a1fbb6bab8" rel="noreferrer noopener nofollow">[email protected]</a>" FromName="test" Subject="test" TextBody="test" />
</root>

更新: 在尝试 abatishchev 的最初建议之后 - 我将问题 #1 的代码更改为以下内容:

var xdoc = XDocument.Parse(contentXml);
return (from element in xdoc.Elements("value")
        select element.Value).FirstOrDefault();

根据提供的信息,它应该返回值节点中的字符串;但是,它返回 null。 xdoc.Elements()(或如上面的代码片段所示)返回 null。

最佳答案

  • 使用XDocument.Parse()创建支持XML声明的文档
  • 要获取单位,请使用

    XDocument.Parse("...").Root // or Element("email")
        .Elements("meta")
            .Elements("smartForm")
                .Elements("unit");
    
  • 或使用 XPath:email/meta/smartForm/unit

  • 您也可以使用查询样式:

    var doc = XDocument.Parse("...");
    var q = from meta in doc.Root.Elements("meta")
            from smartForm in meta.Elements("smartForm")
            from unit in smartForm.Elements("unit")
            select unit;
    

关于c# - 如何访问子 xml 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5783444/

相关文章:

c# - ASP.NET 中组件的许可证

c# - LINQ to Entities - 同一张表上的多个连接

c# - Linq To Sql ManyToMany 更新现有记录

c# - 分布式概率随机数发生器

c# - Asp.net Request.Browser.Crawler - 动态爬虫列表?

c# - 从 PHP 数组转换为 C# 字典

php - Elasticsearch滚动API搜索 “from”

xml - 甲骨文 PL/SQL : Loop through XMLTYPE nodes

java - 如何使用 Java 获取 xml 中元素的第 N 个父元素

c# - 按 LINQ 中的 3 个子字符串组成的字段排序