例外は説明しています.Json.NETは明らかにXmlEntityReference
ノードのJSONへの変換を実装していません。これは&ent;
entity referenceを表すのに使用されるXmlNode
サブタイプです。あなたがそうのように、たとえば、あなたのXMLを読みながらエンティティを拡張する必要があります制限を回避するために
:
var settings = new XmlReaderSettings
{
// Allow processing of DTD
DtdProcessing = DtdProcessing.Parse,
// On older versions of .Net instead set
//ProhibitDtd = false,
// But for security, prevent DOS attacks by limiting the total number of characters that can be expanded to something sane.
MaxCharactersFromEntities = (long)1e7,
// And for security, disable resolution of entities from external documents.
XmlResolver = null,
};
XmlDocument doc = new XmlDocument();
using (var textReader = new StringReader(xml))
using (var xmlReader = XmlReader.Create(textReader, settings))
{
doc.Load(xmlReader);
}
string json = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
注:
それとも、エンティティが常に展開され、セキュリティ設定は、デフォルトでは、より適切である、XDocument
APIに切り替えることができます:
var doc = XDocument.Parse(xml);
string json = JsonConvert.SerializeXNode(doc, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
.Net fiddle作業&ent;
ノードは、その値Sample text
に展開されることを示します:
{
"?xml": {
"@version": "1.0",
"@standalone": "no"
},
"!DOCTYPE": {
"@name": "notes",
"@internalSubset": "\n <!ENTITY ent 'Sample text'>\n "
},
"notes": {
"note": "Sample text"
}
}
出典
2017-12-19 07:03:27
dbc
偉大なフィドルのためにありがとう!これは私の問題を解決しますが、これらのJSONをエンティティで初期XMLにデシリアライズすることが可能かどうかも疑問です。 –
実体、または実体**参照**を意味しますか?エンティティ参照はサポートされていませんが、それらのロジックは['XmlNodeConverter.cs']には表示されません(https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/ XmlNodeConverter.cs)。エンティティの場合は、わかりません。あなたは[mcve]で別の質問をすることができますか? – dbc
@ andrzej1_1 - XmlTextReaderは廃止予定ですので、XmlReader.Create(textReader、settings)を使用するように更新されました。 – dbc