本文介绍了如何在C#中将子节点保存在XML中的同一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下格式的XML:I have an XML in the following format:<?xml-stylesheet href="...css" type="text/css"?><repub> <head> <title>Main Title</title> </head> <body> <sec name="sec_1"> <title>Section Title</title> <break name="article_6-3"> <h1> <page num="6" /> <bl>Article Title</bl> </h1> <fig><img src="images/img_6-3.jpg" alt=""/></fig> <p> <bl> <il>Paragraph italic value</il> and bold value.</bl> </p> <pw>Paragraph value</pw> <fig><img src="images/img_6-4.jpg" alt=""/></fig></break></sec></body></repub> 我希望XML子节点与父节点位于同一行,例如:I want the XML child nodes to be in the same line as the parent, like:<?xml-stylesheet href="...css" type="text/css"?><repub> <head> <title>Main Title</title> </head> <body> <sec name="sec_1"> <title>Section Title</title> <break name="article_6-3"> <h1><page num="6" /><bl>Article Title</bl></h1> //Change 1 <fig><img src="images/img_6-3.jpg" alt=""/></fig> <p><bl><il>Paragraph italic value</il> and bold value.</bl></p> //Change 2 <pw>Paragraph value</pw> <fig><img src="images/img_6-4.jpg" alt=""/></fig></break></sec></body></repub> 创建XML时,每次缩进也出现,我可以尝试以某种方式设法删除缩进,但我完全无法知道如何将子节点保持在同一行中父母。 我不知道该怎么办。请帮忙。 问候阿曼 我尝试过:Upon creating the XML, every time the indent also comes up and I can try to somehow manage to remove the indentation, but I am totally clueless as to how can I keep the child nodes in the same line as the parent.I don't know what to do. Please help.Regards AmanWhat I have tried:XDocument xdoc = new XDocument(); string filecontent = PreProcessXMLnondpvrepub(xmlfile); byte[] myByteArray = System.Text.Encoding.UTF8.GetBytes(filecontent); MemoryStream ms = new MemoryStream(myByteArray); StreamReader sr = new StreamReader(ms); xdoc = XDocument.Load(sr, LoadOptions.PreserveWhitespace); sr.Dispose(); ms.Dispose(); myByteArray = null;private string PreProcessXML(String xmlpath) { StringBuilder sb = new StringBuilder(); string openbreak = ""; string closebreak = ""; string secreplace = ""; string boldreplace = ""; int seccount = 1; foreach (var line in File.ReadLines(xmlpath)) { if (line.ToString() != null) { boldreplace = line.Replace("@@", ""); if (line.Contains("sf@") || line.StartsWith("<psf>") || line.StartsWith("<sf>")) { if (line.Contains("sf@")) { boldreplace = line.Replace("sf@", "<span class=\"standfirst\">"); boldreplace = boldreplace.Replace("</p>", "</span></p>"); } else if (line.StartsWith("<psf>")) { boldreplace = line.Replace("<psf>", "<psf><span class=\"standfirst\">"); boldreplace = boldreplace.Replace("</psf>", "</span></psf>"); } else if (line.StartsWith("<sf>")) { boldreplace = line.Replace("<sf>", "<sf><span class=\"standfirst\">"); boldreplace = boldreplace.Replace("</sf>", "</span></sf>"); } boldreplace = boldreplace.Replace("</sf>", "</span>"); boldreplace = boldreplace.Replace("</psf>", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_bold\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } if (line.Contains("<add>")) { boldreplace = boldreplace.Replace("<add>", "<add>\r\n"); boldreplace = boldreplace.Replace("</add>", "\r\n</add>"); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } if (line.Contains("</add>")) { boldreplace = boldreplace.Replace("<add>", "<add>\r\n"); boldreplace = boldreplace.Replace("</add>", "\r\n</add>"); sb.AppendLine(boldreplace); continue; } if (line.Contains("<p><br/><p>")) { continue; } if (line.Contains("lt@") || line.StartsWith("<plt>")) { if ((line.Contains("") || line.Contains("") || line.Contains("") || line.Contains("<st>") || line.Contains("<sup>") || line.Contains("<sub>"))) { boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } sb.AppendLine(boldreplace); continue; } if (line.Contains("<bl>") || line.StartsWith("<bl>")) { boldreplace = boldreplace.Replace("", "<span class=\"ld_bold\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } if (line.StartsWith("<h1>")) { boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } if (line.StartsWith("<h2") || line.Contains("<h2>")) { boldreplace = boldreplace.Replace("", " "); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } if (line.StartsWith("<h3")) { boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } if (line.StartsWith("<p") || line.StartsWith("<h4") || line.StartsWith("<h5") || line.StartsWith("<h6") || line.StartsWith("bl") && (line.Contains("") || line.Contains("") || line.Contains("") || line.Contains("<st>") || line.Contains("<sup>") || line.Contains("<sub>"))) { boldreplace = boldreplace.Replace("", "<span class=\"ld_bold\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); if (line.Contains("<br/>")) { if (line.Contains("<br/></p>")) { boldreplace = boldreplace.Replace("<br/>", ""); continue; } else { boldreplace = boldreplace.Replace("<br/>", "</p>" + Environment.NewLine + "<p>"); sb.AppendLine(boldreplace); continue; } } sb.AppendLine(boldreplace); continue; } if (line.Contains("<fig>")) { if (line.Contains("fc")) { boldreplace = boldreplace.Replace("<br/>", " "); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } else { boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } } if (line.Contains("<bq>")) { if (line.Contains("<br/>")) { boldreplace = boldreplace.Replace("<br/>", " "); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } else { boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", ""); boldreplace = boldreplace.Replace("", "<span class=\"ld_italic\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("", "<span class=\"ld_underline\">"); boldreplace = boldreplace.Replace("", "</span>"); boldreplace = boldreplace.Replace("<sup>", "<span class=\"ld_superscript\">"); boldreplace = boldreplace.Replace("</sup>", "</span>"); boldreplace = boldreplace.Replace("<sub>", "<span class=\"ld_subscript\">"); boldreplace = boldreplace.Replace("</sub>", "</span>"); boldreplace = boldreplace.Replace("<st>", "<span class=\"ld_strikethrough\">"); boldreplace = boldreplace.Replace("</st>", "</span>"); sb.AppendLine(boldreplace); continue; } } if (line.StartsWith("<sec>")) { secreplace = line.Replace("<sec>", "<sec name=\"sec_" + seccount + "\">"); seccount++; sb.AppendLine(secreplace); continue; } if (line.StartsWith("<break ")) { openbreak = line.Replace("/", ""); if (closebreak != "") { sb.AppendLine(closebreak); } else closebreak = "</break>"; sb.AppendLine(openbreak); } else { if (line == "</sec>") { sb.AppendLine(closebreak); closebreak = ""; } sb.AppendLine(line); } } } return sb.ToString(); }推荐答案You’re doing it wrong way. That’s what you want to achieve is called XSLT Transformations | Microsoft Docs[^]. Here you’ll find very interesting article: Transform From One XML Structure to Another[^] Seems, that basically, you want to add <span someproperty=\"somevalue\">...</span> inside each node, depending on its name. So, a hard-coded version may loop through the collection of XElement[^] or XNode[^] and change its content based on your logic. See:You're doing it wrong way. That's what you want to achieve is called XSLT Transformations | Microsoft Docs[^]. Here you'll find very interesting article: Transform From One XML Structure to Another[^]Seems, that basically, you want to add <span someproperty="somevalue">...</span> inside each node, depending on its name. So, a hard-coded version may loop through the collection of XElement[^] or XNode[^] and change its content based on your logic. See:foreach(XElement xele in xdoc.Root.Descendants("pw")){string tmpVal = xele.Value;xele.Name = "p"; //change pw => pxele.Value = "";xele.Add(new XElement("span", tmpVal)); //add a "span" node!Console.WriteLine("{0}", xele.ToString());} result:result:<p> <span>Paragraph value</span></p> Good luck!Good luck! 这篇关于如何在C#中将子节点保存在XML中的同一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-17 12:45