首页 > 解决方案 > 解析时删除 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.

标签: c#xml

解决方案


上面的答案是正确的,但你可以用更简洁的方式写:

public static string LoadInformation(int index)
{
    return Cleanup(xmlInfographic.DocumentElement.SelectSingleNode("/Infographic/Info/Info" + index).InnerText).Replace("\r\n",string.Empty).Replace(" ", string.Empty);
}


推荐阅读