0

我正在尝试使用 c# 来格式化我的 xml,使其看起来像这样。

<House>
   United States
   <Home>
      NewYork
      Lagos
      California
   </Home>
</House>

有什么办法可以强制缩进吗?它不会缩进没有子元素的任何标记,它会执行以下操作:

<House>
   United States
   <Home>NewYork Lagos California </Home>
</House>

或者

<House>United States </House?
4

3 回答 3

0

如果您可以更改 xml 格式以将所有内容限定在一个元素内,则可以使用以下代码。我将输出粘贴到代码下方;请注意,它不会格式化您的 xml 字符串。

var dodgyXmlString = "<House>United States<Home>NewYork Lagos California</Home></House>";
var validXmlString = "<House><Place>United States</Place><Home>NewYork</Home><Home>Lagos</Home><Home>California</Home></House>";
var strings = new string[] { dodgyXmlString, validXmlString };

foreach(var xmlString in strings)
{    
    var xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(xmlString);
    var sw = new StringWriter();
    var writer = new XmlTextWriter(sw) { Formatting = Formatting.Indented };
    xmlDocument.WriteTo(writer);
    Console.WriteLine(sw.ToString());
}

和输出:

<House>United States<Home>NewYork Lagos California</Home></House>
<House>
  <Place>United States</Place>
  <Home>NewYork</Home>
  <Home>Lagos</Home>
  <Home>California</Home>
</House>
于 2013-07-12T19:00:36.713 回答
0

您必须将换行符常量放入您的值中。就 xml 而言,“NewYork Lagos California”是一个值

于 2013-07-12T18:24:42.743 回答
0

或者,如果你不能这样做或者你可以迭代,你可以这样做:

(“原始”包含您的标记)

string[] spliiter = raw.Split(new char[] { ' ', '\n' });
List<string> splitterList = spliiter.ToList<string>(), xml = new List<string>();
splitterList.RemoveAll(x => x == "");
string temp;
for (int i = 0; i < splitterList.Count() - 1; i++)
{
    temp = "";
    if (StringExtensions.ContainsAll(splitterList[i], new string[] { "<", ">" }))
    {
        temp = splitterList[i];
        xml.Add(temp);
    }
    else if (!splitterList[i].EndsWith("\r"))
    {
        if (StringExtensions.ContainsNone(splitterList[i], new string[] { "<", ">" }))
        {
            if (splitterList[i + 1].EndsWith("\r") || splitterList[i + 1].EndsWith("\r\n"))
            {
                 temp = splitterList[i] + " " + splitterList[i + 1];
                 xml.Add(temp);
                 i++;
             }
        }
    }
    else if (splitterList[i].EndsWith("\r") && StringExtensions.ContainsNone(splitterList[i], new string[] { "<", ">" }))
    {
        if (splitterList[i + 1].EndsWith("\r") && StringExtensions.ContainsNone(splitterList[i + 1], new string[] { "<", ">" }))
        {
            temp = splitterList[i];
            do
            {
                temp = temp + " " + splitterList[i + 1];
                i++;
             } while (splitterList[i + 1].EndsWith("\r") && StringExtensions.ContainsNone(splitterList[i + 1], new string[] { "<", ">" }) || StringExtensions.ContainsNone(splitterList[i + 1], new string[] { "<", ">", "\r", "\n" }));
             temp.Replace('\n', ' ');
             xml.Add(temp);
         }
         else
         {
             temp = splitterList[i];
             xml.Add(temp);
         }
     }
 }
 xml = (xml.Select(s => (s.Replace('\r', ' ').Trim()))).ToList();
 string final_xml = string.Join("\n", xml.ToArray());
于 2013-07-12T20:09:11.173 回答