2012-06-11 3 views
-1

XmlDocument.LoadXml 호출을 위반하는 0x11 16 진수 값이 숨겨진 XML 문자열이 있습니다.문자열에서 16 진수 값 0x11을 제거합니다.

누구든지이 0x11을 찾아서 문자열을 50000 문자로 반복하지 않고 찾아내는 방법을 알려주실 수 있습니까?

덕분에

답변

1

내가 전에이 작업을 수행하기 위해 필요한 것, 그리고 여기 내 코드는 그대로입니다. 문제가되는 문자를 찾으려면 LineNumber 및 LinePosition 속성을 읽습니다.

ko-kr으로 만 테스트되었지만 예외 메시지에서 0x 만 찾는 것이므로이 점이 확실하지 않습니다.

internal static XmlDocument ParseWithRetry(ref string xml, string errorComment, int badCharRetryCount, Action<StringBuilder,XmlException,string> onXmlExceptionDelegate) 
    { 
    StringBuilder xmlBuff = null; 
    if (badCharRetryCount < 0) 
     badCharRetryCount = 0; 
    XmlDocument doc = new XmlDocument(); 
    int attemptCount = badCharRetryCount + 1; 
    for (int i = 0; i < attemptCount; i++) 
    { 
     try 
     { 
      doc.LoadXml(xml); 
      break; 
     } 
     catch (XmlException xe) 
     { 
      if (xe.Message.Contains("0x")) 
      { 
       if (xmlBuff == null) 
       xmlBuff = new StringBuilder(xml); 
       // else, it's already synchronized with xml... no need to create a new buffer. 

       // Write to the log... or whatever the caller wants to do. 
       if (onXmlExceptionDelegate != null) 
       onXmlExceptionDelegate (xmlBuff, xe, errorComment); 

       // Remove the offending character and try again. 
       int badCharPosition = GetCharacterPosition (xml, xe.LineNumber, xe.LinePosition); 
       if (badCharPosition >= 0) 
       xmlBuff.Remove(badCharPosition, 1); 
       xml = xmlBuff.ToString(); 
       continue; 
      } 
      throw; 
     } 
    } 

    return doc; 
    } 

    static readonly char[] LineBreakCharacters = { '\r', '\n' }; 
    internal static int GetCharacterPosition (string xml, int lineNumber, int linePosition) 
    { 
    // LineNumber is one-based, not zero based. 
    if (lineNumber == 1) 
     return linePosition - 1; 

    int pos = -1; 
    // Skip to the appropriate line number. 
    for (int i = 1; i < lineNumber; i++) 
    { 
     pos = xml.IndexOfAny(LineBreakCharacters, pos + 1); 
     if (pos < 0) 
      return pos; // bummer.. couldn't find it. 
     if (xml[pos] == '\r' && pos + 1 < xml.Length && xml[pos + 1] == '\n') 
      pos++; // The CR is followed by a LF, so treat it as one line break, not two. 
    } 
    pos += linePosition; 
    return pos; 
    } 
+0

대단한 스크립트 ... 너 락 !! – Nugs