Как сохранить дочерние узлы в одной строке в XML на языке C#?
У меня есть XML в следующем формате:
<?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 находились в той же строке, что и родительские, например:
<?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 каждый раз появляется отступ, и я могу попытаться каким-то образом удалить отступ, но я совершенно не знаю, как я могу держать дочерние узлы в той же строке, что и родительский.
Я не знаю, что делать. Пожалуйста помочь.
С Уважением Аман
Что я уже пробовал:
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(); }
Maciej Los
Есть ли какая-то причина, по которой вы хотите изменить нормальное поведение XML-узлов?
Кстати: покажите метод, который вы используете для сохранения xml-файла...
Primo Chalice
Да, на самом деле я работаю над проектом, и есть некоторые необходимые изменения, которые должны быть сделаны до окончательного вывода. Несколько типов вывода имеют несколько вариантов.
Например, &ЛТ;бл&ГТ; превращается в < промежуток класс="ld_bold"> и < ПВ&ГТ; будет &ЛТ; п&ГТ;.
Поэтому, если он находится в одной строке, я могу использовать StringBuilder и заменить значения.
Maciej Los
Покажите метод, который вы используете для сохранения xml-файла...
Вам нужно добавить больше деталей для окончательного преобразования xml.
Primo Chalice
Поскольку код длинный, я предоставляю ссылку на контент.
https://1drv.ms/u/s!ApO52Imsl0YIia4w2dssTKdBl7Rnig
Maciej Los
Извините, но никто не будет скачивать исходный код из неизвестного источника. И никто не хочет изучать чей-то проект. Вам нужно кратко объяснить, чего вы хотите достичь и почему это не работает для вас. Вы также должны опубликовать соответствующий код. Воспользуйся "Улучшить вопрос" виджет.