在C#中,如何在没有额外换行符和制表符的情况下解析XML内容?

在C#中,如何在没有额外换行符和制表符的情况下解析XML内容?,c#,.net,xml,C#,.net,Xml,我使用以下xml文件读取需要写入的文件的内容: <properties> <url>http://www.leagueoflegends.com/service-status</url> <content>host=beta.lol.riotgames.com xmpp_server_url=chat.na.lol.riotgames.com lobbyLandingURL=http://www.leagueoflegends.co

我使用以下xml文件读取需要写入的文件的内容:

<properties>
    <url>http://www.leagueoflegends.com/service-status</url>
    <content>host=beta.lol.riotgames.com
xmpp_server_url=chat.na.lol.riotgames.com
lobbyLandingURL=http://www.leagueoflegends.com/pvpnet_landing
ladderURL=http://www.leagueoflegends.com/ladders
storyPageURL=http://www.leagueoflegends.com/story
lq_uri=https://lq.na.lol.riotgames.com/login-queue/rest/queue</content>
</properties>
文件是文件的路径,因为我可以读取其他所有内容

任何想法都将不胜感激

提前谢谢

编辑: 前面的PropertyFile类代码(内容只是一个字符串,其中包含我正在读取的文件类型的一个文件的数据):

所需的文件输出为:

host=beta.lol.riotgames.com
xmpp_server_url=chat.na.lol.riotgames.com
lobbyLandingURL=http://www.leagueoflegends.com/pvpnet_landing
ladderURL=http://www.leagueoflegends.com/ladders
storyPageURL=http://www.leagueoflegends.com/story
lq_uri=https://lq.na.lol.riotgames.com/login-queue/rest/queue

尝试将LoadOptions设置为None:

XDocument root = XDocument.Parse(File.ReadAllText(file), LoadOptions.None);
根据MSDN,这将忽略所有不重要的空白

我尝试了它(仅使用XML的字符串-我没有从文件中加载它),得到了以下结果:

host=beta.lol.riotgames.com xmpp_server_url=chat.na.lol.riotgames.com lobbyLandingURL=http://www.leagueoflegends.com/pvpnet_landing 阶梯=http://www.leagueoflegends.com/ladders 故事页面URL=http://www.leagueoflegends.com/story lq_-uri=https://lq.na.lol.riotgames.com/login-queue/rest/queue

这就是你要找的吗

编辑以添加我使用的代码

string xml = @"<properties>
<url>http://www.leagueoflegends.com/service-status</url>
<content>host=beta.lol.riotgames.com

尝试将LoadOptions设置为None:

XDocument root = XDocument.Parse(File.ReadAllText(file), LoadOptions.None);
根据MSDN,这将忽略所有不重要的空白

我尝试了它(仅使用XML的字符串-我没有从文件中加载它),得到了以下结果:

host=beta.lol.riotgames.com xmpp_server_url=chat.na.lol.riotgames.com lobbyLandingURL=http://www.leagueoflegends.com/pvpnet_landing 阶梯=http://www.leagueoflegends.com/ladders 故事页面URL=http://www.leagueoflegends.com/story lq_-uri=https://lq.na.lol.riotgames.com/login-queue/rest/queue

这就是你要找的吗

编辑以添加我使用的代码

string xml = @"<properties>
<url>http://www.leagueoflegends.com/service-status</url>
<content>host=beta.lol.riotgames.com

默认情况下,XDocument/XElement的序列化将格式化(即缩进)xml片段

尝试使用:

root.Save("Root.xml", SaveOptions.DisableFormatting);

但是,任何DOM操作都不会涉及不重要的空格/选项卡。在元素中写入的内容就是您得到的内容。

默认情况下,XDocument/XElement格式的序列化(即缩进)是xml片段

尝试使用:

root.Save("Root.xml", SaveOptions.DisableFormatting);

但是,任何DOM操作都不会涉及不重要的空格/选项卡。你在元素中写的东西就是你得到的东西。

如果你尝试…,你会得到什么

using System.IO;

XDocument root = XDocument.Parse(File.ReadAllText(file),
                              LoadOptions.PreserveWhitespace); 
string content = (from Content in root.Descendants("content")
                   select (string)Content.Value).Single();
File.WriteAllText("SomeTempFile.txt", content);

我怀疑这个文本文件的格式将与您期望的一样。这表明
属性文件有问题

如果您尝试…,会得到什么

using System.IO;

XDocument root = XDocument.Parse(File.ReadAllText(file),
                              LoadOptions.PreserveWhitespace); 
string content = (from Content in root.Descendants("content")
                   select (string)Content.Value).Single();
File.WriteAllText("SomeTempFile.txt", content);

我怀疑这个文本文件的格式将与您期望的一样。这表明
PropertyFile

什么是
PropertyFile
?我建议这是额外空白的likley插入器。嗯,不,我想这会造成混乱,所以我会添加我已经忘记的类。它只是一个数据持有者。什么是
PropertyFile
?我建议这是额外空白的likley插入器。嗯,不,我想这会造成混乱,所以我会添加我已经忘记的类。它只是一个数据保存器。嗯……我的部分代码格式不正确,但我不知道如何让它这样做。我复制并粘贴了你在问题中的内容到我的测试程序中。有两个问题,第一个问题就是你所说的——我需要加载选项。无。另一个是。。。我弄乱了一些路径,所以我读的是一个旧的xml,它有那些标签和换行符。你的回答仍然是解决问题的重要方面,所以我希望其他人不介意。除此之外,谢谢大家!嗯…我的部分代码格式不正确,但我不知道如何让它这样做。我复制并粘贴了你在问题中的内容到我的测试程序中。有两个问题,第一个问题就是你所说的——我需要加载选项。无。另一个是。。。我弄乱了一些路径,所以我读的是一个旧的xml,它有那些标签和换行符。你的回答仍然是解决问题的重要方面,所以我希望其他人不介意。除此之外,谢谢大家!有趣的是…我更新了我的测试程序,从一个文件加载,然后保存到一个文件中-我使用LoadOptions.PreserveWhiteSpace和LoadOptions.None获得了所需的结果。我已经过了就寝时间…明天我会好奇地想看看答案。有趣的是…我更新了我的测试程序,从一个文件加载,然后保存到一个文件-我用LoadOptions.PreserveWhiteSpace和LoadOptions.None得到了想要的结果。过了我的就寝时间…我明天会好奇地想知道答案。