gpt4 book ai didi

c# - 使用 HTML 标签反序列化 XML

转载 作者:太空宇宙 更新时间:2023-11-03 20:12:12 25 4
gpt4 key购买 nike

我正在尝试反序列化一个 XML 文件,它工作正常,除了包含 HTML 标记的节点。以下是 XML 文件的片段:

<article mdate="2011-12-29" key="tr/trier/MI99-02" publtype="informal publication">
<author>Friedemann Leibfritz</author>
<title>A LMI-Based Algorithm for Designing Suboptimal Static H<sub>2</sub>/H<sub>infinity</sub> Output Feedback Controllers</title>
<journal>Universit&auml;t Trier, Mathematik/Informatik, Forschungsbericht</journal>
<volume>99-02</volume>
<year>1999</year>
</article>

然后,我得到了错误:

{"Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content. Line 1148, position 64."}

错误发生在:

A LMI-Based Algorithm for Designing Suboptimal Static H2/Hinfinity Output Feedback Controllers

where HTML tags sub and /sub exist.

Is there a way to deserialize the title node as a whole, ignoring the HTML tags? Below is a portion of my code:

XmlReaderSettings readerSettings = new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Parse,
XmlResolver = new LocalXhtmlXmlResolver()
};

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "dblp";
xRoot.IsNullable = true;
XmlSerializer deserializer;
XmlReader textReader;

deserializer = new XmlSerializer(typeof(List<Entity.Article>), xRoot);
textReader = XmlReader.Create(xmlPath, readerSettings);
List<Entity.Article> articleList;
articleList = (List<Entity.Article>)deserializer.Deserialize(textReader);
textReader.Close();

非常感谢任何帮助 - 谢谢!

最佳答案

您的 XML 未正确转义。解析器无法知道这些标记不是 XML 文档的一部分,当它们被这样对待时,您的 XML 是无效的,因为一个元素嵌套在另一个元素的值中。

正确转义的 XML 片段将是

<article mdate="2011-12-29" key="tr/trier/MI99-02" publtype="informal publication">
<author>Friedemann Leibfritz</author>
<title>A LMI-Based Algorithm for Designing Suboptimal Static H&lt;sub&gt;2&lt;/sub&gt;/H&lt;sub&gt;infinity&lt;/sub&gt; Output Feedback Controllers</title>
<journal>Universit&auml;t Trier, Mathematik/Informatik, Forschungsbericht</journal>
<volume>99-02</volume>
<year>1999</year>
</article>

关于c# - 使用 HTML 标签反序列化 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19287666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com