1

アプリケーションは XML ファイルを処理する必要があります。次のような値を持つ XML を受け取ることがあります。

<DiagnosisStatement>
     <StmtText>ST &</StmtText>
</DiagnosisStatement>

&<アプリケーションが XML を正しくロードできず、次のように例外がスローされるためです。

An error occurred while parsing EntityName. Line 92, position 24.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.Throw(String res)
   at System.Xml.XmlTextReaderImpl.ParseEntityName()
   at System.Xml.XmlTextReaderImpl.ParseEntityReference()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
   at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
   at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
   at System.Xml.XmlDocument.Load(XmlReader reader)
   at System.Xml.XmlDocument.Load(String filename)
   at Transformation.GetEcgTransformer(String filePath, String fileType, String Manufacture, String Producer) in D:\Transformation.cs:line 160

&<XML が例外なく正常に処理されるように、出現するすべての を 'and<' に置き換える必要があります。

4

2 に答える 2

5

これは、Botz3000からの回答を利用してXMLをロードするために行ったことです。

string oldText = File.ReadAllText(filePath);
string newText = oldText.Replace("&<", "and<");
File.WriteAllText(filePath, newText, Encoding.UTF8);
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
于 2013-03-20T12:21:39.607 に答える
2

&としてエスケープする必要があるため、XML ファイルは無効です&amp;。そのため、エラーを発生させずに xml をロードすることはできません。ただし、ファイルをプレーンテキストとしてロードすると、それを実行できます。

string invalid = File.ReadAllText(filename);
string valid = invalid.Replace("&<", "and<");
File.WriteAllText(filename, valid);

ただし、Xml ファイルの生成方法を制御できる場合は、&as をエスケープするか、あなたが言ったよう&amp;に置き換えることで、その問題を修正する必要があり"and"ます。

于 2013-03-01T08:18:40.457 に答える