我在旧数据库中保存了一个 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><?xml version="1.0" encoding="utf-16"?><root><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" /><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" /></root></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/