c# - 解析时删除 XML 空格和断行

标签 c# xml

<分区>

我试图将 XML 作为字符串获取,但所有空格都出现在最终结果中,我尝试了 Regex 和此处的许多答案,但我没有成功。这是我的代码。

public class XMLManager
{
    private static XmlDocument xmlInfographic;
    private static TextAsset infographicXML;

    public XMLManager()
    {
        infographicXML = Resources.Load("XML/Infographic") as TextAsset;
        xmlInfographic = new XmlDocument();
        xmlInfographic.LoadXml(infographicXML.text);
    }

     public static string LoadInformation(int index)
     {
          return xmlInfographic.DocumentElement.SelectSingleNode("/Infographic/Info/Info" + index).InnerText;
     }
}

这里是 XML 文件:

<Infographic>

  <Info>
    <Info0>
      • Something_0.
      • Something_0.
      • Something_0.
      • Something_0.
      • Something_0.
    </Info0>

    <Info1>
      • Something_1.
      • Something_1.
      • Something_1.
   </Info1>

  </Info>

</Infographic>

当前的输出是这样的:

\r\n
      • Someting_1.
      • Someting_1.
      • Someting_1.
\r\n

我需要的是:

• Someting_1.
• Someting_1.
• Someting_1.

最佳答案

您可以用空字符替换新行和空格。

public class XMLManager
{
    private static XmlDocument xmlInfographic;
    private static TextAsset infographicXML;

    public XMLManager()
    {
        infographicXML = Resources.Load("XML/Infographic") as TextAsset;
        xmlInfographic = new XmlDocument();
        xmlInfographic.LoadXml(infographicXML.text);
    }

    public static string LoadInformation(int index)
    {
        return Cleanup(xmlInfographic.DocumentElement.SelectSingleNode("/Infographic/Info/Info" + index).InnerText);
    }

    private static string Cleanup(string input)
    {
        return input
            .Replace("\n", string.Empty)
            .Replace(" ", string.Empty)
            .Replace("\r", string.Empty); 
    }
}

关于c# - 解析时删除 XML 空格和断行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55988622/

相关文章:

c# - 如何访问 Perfmon 信息?

c# - 如何在 LINQ 中对一个未指定的数字使用多个 OrderBy?

python - 在Python中使用For循环创建一个xml文件

sql - 在 PostgreSQL 的 where 子句中使用 XML 列

Javascript 数组和信息检索

java - 如何将具有重复元素的 XML 转换为 Java 对象 (JAXB)

c# - 如何在 Picturebox C# 中添加标签透明度?

c# - ASP.NET MVC。如何使用 DisplayNameFor 来创建表格标题和正文?

C# 关闭窗口时复选框选中/取消选中

sql-server - 设计用于在 SQL Server 2008 R2 中存储 XML 数据的表