Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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_Format - Fatal编程技术网

C#创建格式化的xml

C#创建格式化的xml,c#,xml,format,C#,Xml,Format,我正在尝试使用c#来格式化xml,使其看起来像这样 <House> United States <Home> NewYork Lagos California </Home> </House> 美国 纽约 拉各斯 加利福尼亚 有什么办法我可以强迫你留下凹痕吗?它不会缩进没有子元素的任何标记,并执行如下操作: <House> United States <Hom

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

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

美国
纽约
拉各斯
加利福尼亚
有什么办法我可以强迫你留下凹痕吗?它不会缩进没有子元素的任何标记,并执行如下操作:

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

美国
纽约拉各斯加利福尼亚州


美国您必须将换行常量放入您的值中。就xml而言,“纽约
拉各斯

“California”是一个值

如果可以将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());
}
var dodgyXmlString=“美国加利福尼亚州拉各斯市”;
var validXmlString=“美国newyorklagoscalifornia”;
var strings=新字符串[]{dodgyXmlString,validXmlString};
foreach(字符串中的var xmlString)
{    
var xmlDocument=新的xmlDocument();
LoadXml(xmlString);
var sw=新的StringWriter();
var writer=newxmltextwriter(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>
美国加利福尼亚州拉各斯
美国
纽约
拉各斯
加利福尼亚

或者,如果您不能这样做,或者您可以进行迭代,您可以这样做:

(“raw”包含您的标记)

string[]spliiter=raw.Split(新字符[]{'','\n'});
List splitterList=spliiter.ToList(),xml=new List();
splitterList.RemoveAll(x=>x==“”);
字符串温度;
对于(int i=0;i(s.Replace('\r','.Trim())).ToList();
string final_xml=string.Join(“\n”,xml.ToArray());
<House>United States<Home>NewYork Lagos California</Home></House>
<House>
  <Place>United States</Place>
  <Home>NewYork</Home>
  <Home>Lagos</Home>
  <Home>California</Home>
</House>
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());