2013-03-01 5 views
1

응용 프로그램에서 XML 파일을 처리해야합니다. &<의 내 응용 프로그램 XML 제대로 던지는 예외를로드 할 수 없습니다XML 파일의 값 바꾸기

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

때문에 다음과 같이 :

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 

가 지금은 모든 항목을 교체 할 필요가 다음과 같이 몇 번 우리는 값 XMLS을받을 &<을 '및 <'으로 설정하면 XML을 예외없이 성공적으로 처리 할 수 ​​있습니다.

답변

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); 
2

&&amp;으로 이스케이프 처리해야하므로 Xml 파일이 유효하지 않으므로 오류가 발생하지 않고 xml을로드 할 수 없습니다. XML 파일이 있지만 생성되는 방법을 제어 할 수있는 경우

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

, 당신은 하나가 &amp;&을 탈출하거나 교체하여이 문제를 해결해야한다 : 당신이 비록 일반 텍스트로 파일을로드하는 경우 당신은 그것을 할 수 있습니다 당신이 말한대로 "and"으로.