XML 清理(从属性值中删除无效字符) [英] XML Clean up (remove invalid characters from attribute value)

查看:30
本文介绍了XML 清理(从属性值中删除无效字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 xml 中删除无效字符但保持标准例如我想删除所有 <和 " 来自属性值内部字符串

How can i remove none valid chars from xml but keep standard for example i want remove all < and " from attribute value inner strings

<log>
  <data id="1" name="No Error"  value="0" />
  <data id="2" name="Error "1" between text" value="0" />
  <data id="3" name="Error <2> between text"  value="0"  />
</log>

我如何每天删除围绕1"的引号和 <> 环绕 2

How can i daynamicly remove quotes surrounds "1" and <> surrounds 2

最终结果应该是

<log>
  <data id="1" name="No Error"  value="0"  />
  <data id="2" name="Error 1 between text" value="0" />
  <data id="3" name="Error 2 between text"  value="0"  />
</log>

感谢支持

我正在考虑以下解决方案:

I was thinking of the following solution:

  1. 以文本形式读取文件
  2. 修改任何以 开头并以
  3. 删除所有",<,>
  4. 之后添加",在之前添加"
  1. Read the file as text
  2. Modify any string that starts with <Name=> and ends with <value=>
  3. remove all ",<,>
  4. add " after <name=> and add " before <value=>

如果这是正确的,我如何用 C# 来做到这一点,替换方法将不起作用.

if this is correct, how can i do this with C#, the replace method will not work.

谢谢

推荐答案

为了您的信息,我找到了 2 种不同的方法,

for your information I found 2 different ways,

1-

public static void ReplaceInvalidCharFromAttribute(string filePath, string startElement, string nextElement, string[] removeStrings)
        {
            string tempFile = Path.GetTempFileName();

            using (var sr = new StreamReader(filePath))
            {
                using (var sw = new StreamWriter(tempFile))
                {
                    string line;
                    string temp;
                    while ((line = sr.ReadLine()) != null)
                    {
                        temp = RemoveInvalidCharFromAttribute(line, startElement, nextElement, removeStrings);
                        sw.WriteLine(temp??line);
                    }
                }
            }

            File.Delete(filePath);
            File.Move(tempFile, filePath);
        }



public static string RemoveInvalidCharFromAttribute(string input, string startElement, string nextElement, string[] invalidChars)
        {
            if (input.IndexOf(startElement) < 0 || input.IndexOf(nextElement) < 0) return null;
            int start =input.IndexOf(startElement) + startElement.Length;
            int end = input.IndexOf(nextElement);
            StringBuilder res = new StringBuilder(input.Substring(start, (end - start)));
            StringBuilder resCopy = new StringBuilder(res.ToString());

            foreach (string inv in invalidChars)
                res.Replace(inv, "");

            // return the result after surrounding the text with double 
            return
                input.Replace(
                resCopy.ToString(),
                (String.Concat("\"", String.Concat(res.ToString().Trim(), "\" "))));
        }

2- http://support.microsoft.com/kb/316063

太好了,谢谢

这篇关于XML 清理(从属性值中删除无效字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆