Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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# 解析时删除XML空格和换行符_C#_Xml - Fatal编程技术网

C# 解析时删除XML空格和换行符

C# 解析时删除XML空格和换行符,c#,xml,C#,Xml,我试图将XML作为字符串获取,但所有空格都出现在最终结果中,我尝试了正则表达式和这里的许多答案,但没有成功。这是我的密码 public class XMLManager { private static XmlDocument xmlInfographic; private static TextAsset infographicXML; public XMLManager() { infographicXML = Resources.Load(

我试图将XML作为字符串获取,但所有空格都出现在最终结果中,我尝试了正则表达式和这里的许多答案,但没有成功。这是我的密码

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>
我需要的是:

• 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); 
    }
}

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

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

请把饰件()放在适当的位置

返回xmlInfographic.DocumentElement.SelectSingleNode(“/Infographic/Info/Info”+索引)

应该是


返回xmlInfographic.DocumentElement.SelectSingleNode(“/Infographic/Info/Info”+索引).InnerText.trim()

这将把
something 1
转换成
something 1
。很可能不是期望的输出
public static string LoadInformation(int index)
{
    return Cleanup(xmlInfographic.DocumentElement.SelectSingleNode("/Infographic/Info/Info" + index).InnerText).Replace("\r\n",string.Empty).Replace(" ", string.Empty);
}