如何访问子 xml 元素?

发布于 2024-11-03 10:22:52 字数 3662 浏览 1 评论 0原文

我有一个 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="[email protected]" FromEmail="[email protected]" FromName="password test" Subject="password test" TextBody="info" /&gt;&lt;ForgotUsername BccEmail="[email protected]" FromEmail="[email protected]" FromName="test" Subject="test" TextBody="test" /&gt;&lt;/root&gt;</value>
</email>

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

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

我做错了什么?

实体替换后 Value 的 XML:

<root>
    <ForgotPassword BccEmail="[email protected]" FromEmail="[email protected]" FromName="password test" Subject="password test" TextBody="info" />
    <ForgotUsername BccEmail="[email protected]" FromEmail="[email protected]" 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。

I have an xml string saved in a legacy database that I'm attempting to parse. I can get the string but having 2 issues getting the values that I need. First, some sample 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><?xml version="1.0" encoding="utf-16"?><root><ForgotPassword BccEmail="[email protected]" FromEmail="[email protected]" FromName="password test" Subject="password test" TextBody="info" /><ForgotUsername BccEmail="[email protected]" FromEmail="[email protected]" FromName="test" Subject="test" TextBody="test" /></root></value>
</email>

Issue #1 - I attempted to parse the xml using XElement.Parse("string") however, I cannot get the <value> node unless I remove the xml declaration (i.e. first 39 characters). I'm hoping to not HAVE to do this since it's a brittle solution.

Issue #2 - Once I have the <value> element's contents and parsed into an XElement, I want the query to either the <ForgotUsername> or <ForgotPassword> child node of the <root> document element. When I got .Elements(), I'm told the collection is null.

What am I doing wrong?

The Value's XML after entities are replaced:

<root>
    <ForgotPassword BccEmail="[email protected]" FromEmail="[email protected]" FromName="password test" Subject="password test" TextBody="info" />
    <ForgotUsername BccEmail="[email protected]" FromEmail="[email protected]" FromName="test" Subject="test" TextBody="test" />
</root>

UPDATES:
After trying abatishchev's initial suggestion - I changed the code for issue #1 to the following:

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

Based on the information provided, it should return the string in the value node; however, it's returning null. The xdoc.Elements() (or as shown in the above snippet) return null.

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

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

发布评论

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

评论(3

高跟鞋的旋律 2024-11-10 10:22:52
  • 使用 XDocument.Parse() 创建支持 XML 声明的文档
  • 要获取单位,请使用

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

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

    var doc = XDocument.Parse("...");
    var q = 来自 doc.Root.Elements("meta") 中的元
            来自meta.Elements(“smartForm”)中的smartForm
            来自 smartForm.Elements("unit") 中的单元
            选择单位;
    
  • Use XDocument.Parse() to create a document supporting XML declaration
  • To get units, use

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

  • also you can use query-style:

    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;
    
笑红尘 2024-11-10 10:22:52

以下内容对我有用:

var xd = XDocument.Load("Test.xml");
var xv = XDocument.Parse((string)xd.Root.Element("value"));
Console.WriteLine(xv.Root.Elements().Count());

对于 ForgotPassword 和 ForgotUsername,输出为 2。

The following works for me:

var xd = XDocument.Load("Test.xml");
var xv = XDocument.Parse((string)xd.Root.Element("value"));
Console.WriteLine(xv.Root.Elements().Count());

Output is 2, for ForgotPassword and ForgotUsername.

千纸鹤带着心事 2024-11-10 10:22:52

对于#1:

XElement doc = XElement.Load(file);
XElement valueElement = doc.Element("value");
string value = (string)valueElement;

For #1:

XElement doc = XElement.Load(file);
XElement valueElement = doc.Element("value");
string value = (string)valueElement;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文