如何访问子 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><?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>
问题 #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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XDocument.Parse()
创建支持 XML 声明的文档要获取单位,请使用
或使用 XPath:
email/meta/smartForm/unit
您也可以使用查询样式:
XDocument.Parse()
to create a document supporting XML declarationTo get units, use
or use XPath:
email/meta/smartForm/unit
also you can use query-style:
以下内容对我有用:
对于 ForgotPassword 和 ForgotUsername,输出为 2。
The following works for me:
Output is 2, for ForgotPassword and ForgotUsername.
对于#1:
For #1: