2012-06-11 75 views
-1

我有一個隱藏在其中的0x11十六進制值的XML字符串,它正在打破我的XmlDocument.LoadXml調用。從字符串中刪除十六進制值0x11

有人可以告訴我如何找到並銷燬0x11,而不是循環遍歷所有50000字符串的字符串。

感謝

回答

1

我需要之前做到這一點,這裏是我的代碼逐字。它讀取LineNumber和LinePosition屬性以查找違規字符。

它只在en-US進行測試,但我不確定這是否重要,因爲它只在異常消息中尋找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

相關問題