Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:将具有超过10k行的xml加载到XmlDocument变量时删除特殊字符_C# - Fatal编程技术网

C#:将具有超过10k行的xml加载到XmlDocument变量时删除特殊字符

C#:将具有超过10k行的xml加载到XmlDocument变量时删除特殊字符,c#,C#,下面的代码在xml具有“&”和“”时不起作用 事情已经尝试过了: HugeXmlVariable = System.Security.SecurityElement.Escape(HugeXmlVariable); HugeXmlVariable = HugeXmlVariable.ToSTrein().Replace("&", string.Empty).Replace(">", "&gt;").Replace("<", "&lt;").Replace("

下面的代码在xml具有“&”和“”时不起作用

事情已经尝试过了:

HugeXmlVariable = System.Security.SecurityElement.Escape(HugeXmlVariable);

HugeXmlVariable = HugeXmlVariable.ToSTrein().Replace("&", string.Empty).Replace(">", "&gt;").Replace("<", "&lt;").Replace("'", "&apos;").Replace("\"", "&quot;").ToString();

HugeXmlVariable = XmlConvert.DecodeName(HugeXmlVariable);
HugeXmlVariable=System.Security.SecurityElement.Escape(HugeXmlVariable);

HugeXmlVariable=HugeXmlVariable.ToSTrein().Replace(&),string.Empty.Replace(“>”,”).Replace(你能试试下面的方法吗

    public static string RemoveSpecialCharacters(string str)
    {
        StringBuilder sb = new StringBuilder();
        foreach (char c in str)
        {
            if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')
            {
                sb.Append(c);
            }
        }
        return sb.ToString();
    }
公共静态字符串删除特殊字符(字符串str)
{
StringBuilder sb=新的StringBuilder();
foreach(str中的字符c)
{

如果((c>='0'&&c='A'&&c='A'&&c如果XML无效,我们可以将其作为文本读取,然后将替换的文本解析到流中。然后从流中加载有效的XML字符串。

string filepaht="your xml path";
string data=File.ReadAllText(filepath);
Stream newdata=null;

 StreamReader strReader = null;
            try
            {
                //Lookup the encoding. If we not find the specified encoding, we use UTF-8.

               Encoding  messageEncoding = Encoding.UTF8;

                strReader = new StreamReader(data, messageEncoding);

                // Read the entire message into a string. Not a good idea if you receive largeish messages. Then you ned a bufferen approach
                string msgBodyContentString = strReader.ReadToEnd();

                string[] searchStrings = SearchString.Split(STRING_SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string[] replaceStrings = ReplaceString.Split(STRING_SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                for (int index = 0; index <= (searchStrings.Length -1); index++)
                {
                    string oldValue = searchStrings[index];
                    if (!string.IsNullOrEmpty(oldValue))
                    {
                        //default to string empty if we do not have a replace value
                        string newValue = string.Empty;
                        if (index <= replaceStrings.Length - 1)
                            newValue = replaceStrings[index];

                        msgBodyContentString = msgBodyContentString.Replace(Regex.Unescape(oldValue), Regex.Unescape(newValue));
                    }
                }

                // Convert to bytes
                byte[] msgBodyContentBytes = Encoding.Unicode.GetBytes(msgBodyContentString);

                // Convert the encoding
                byte[] msgBodyContentBytesConverted = Encoding.Convert(Encoding.Unicode, messageEncoding, msgBodyContentBytes);

                // Write new stream back
                newdata = new MemoryStream(msgBodyContentBytesConverted);                                        
            }


var inputXml = XDocument.ReadFrom(newdata);
string filepaht=“您的xml路径”;
字符串数据=File.ReadAllText(文件路径);
流newdata=null;
StreamReader strReader=null;
尝试
{
//查找编码。如果找不到指定的编码,则使用UTF-8。
Encoding messageEncoding=Encoding.UTF8;
strReader=新的StreamReader(数据、消息编码);
//将整个消息读入一个字符串。如果您收到大量消息,这不是一个好主意。然后您使用了bufferen方法
字符串msgBodyContentString=strReader.ReadToEnd();
string[]searchStrings=SearchString.Split(string_SEPARATOR.ToCharArray(),StringSplitOptions.removeMptyEntries);
string[]replaceStrings=ReplaceString.Split(string_SEPARATOR.tocharray(),StringSplitOptions.removeMptyEntries);

对于(int index=0;index请提供相关XML的一个小样本。如果XML无效,则除了修复它之外,没有其他解决方案。XML是如何生成的?如果您有任何未替换的字符,则首先将XML解析为非XML时会遇到重大问题。@DanWilson-pos中更新的XMLt XML无效,因为元素值中的
&
应该是
&;
@DanWilson我无法更正输入的XML,我已经修复了它,并且我尝试用它替换检查每个字符它将花费大量时间。
    public static string RemoveSpecialCharacters(string str)
    {
        StringBuilder sb = new StringBuilder();
        foreach (char c in str)
        {
            if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')
            {
                sb.Append(c);
            }
        }
        return sb.ToString();
    }
string filepaht="your xml path";
string data=File.ReadAllText(filepath);
Stream newdata=null;

 StreamReader strReader = null;
            try
            {
                //Lookup the encoding. If we not find the specified encoding, we use UTF-8.

               Encoding  messageEncoding = Encoding.UTF8;

                strReader = new StreamReader(data, messageEncoding);

                // Read the entire message into a string. Not a good idea if you receive largeish messages. Then you ned a bufferen approach
                string msgBodyContentString = strReader.ReadToEnd();

                string[] searchStrings = SearchString.Split(STRING_SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string[] replaceStrings = ReplaceString.Split(STRING_SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                for (int index = 0; index <= (searchStrings.Length -1); index++)
                {
                    string oldValue = searchStrings[index];
                    if (!string.IsNullOrEmpty(oldValue))
                    {
                        //default to string empty if we do not have a replace value
                        string newValue = string.Empty;
                        if (index <= replaceStrings.Length - 1)
                            newValue = replaceStrings[index];

                        msgBodyContentString = msgBodyContentString.Replace(Regex.Unescape(oldValue), Regex.Unescape(newValue));
                    }
                }

                // Convert to bytes
                byte[] msgBodyContentBytes = Encoding.Unicode.GetBytes(msgBodyContentString);

                // Convert the encoding
                byte[] msgBodyContentBytesConverted = Encoding.Convert(Encoding.Unicode, messageEncoding, msgBodyContentBytes);

                // Write new stream back
                newdata = new MemoryStream(msgBodyContentBytesConverted);                                        
            }


var inputXml = XDocument.ReadFrom(newdata);