Come posso modificare questo codice StringBuilder-to-XML in LINQ-to-XML?
-
06-07-2019 - |
Domanda
Nella mia applicazione ho creato un file XML con questo codice usando StringBuilder :
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine);
sb.Append(String.Format("<{0}>{1}", _pluralCamelNotation, Environment.NewLine));
for (int index = 0; index < 3; index++)
{
sb.Append(String.Format("\t<{0}>{1}", _singularCamelNotation, Environment.NewLine));
foreach (DataType dataType in _allDataTypes)
{
sb.Append(String.Format("\t\t<{0}>{2}</{0}>{1}", dataType.CamelCaseNotation, Environment.NewLine, dataType.GetDummyData()));
}
sb.Append(String.Format("\t</{0}>{1}", _singularCamelNotation, Environment.NewLine));
}
sb.Append(String.Format("</{0}>{1}", _pluralCamelNotation, Environment.NewLine));
return sb.ToString();
Come posso fare la stessa cosa con LINQ , qualcosa del genere:
PSEUDO-CODE:
var xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
for (int index = 0; index < 3; index++) {
new XElement(_pluralCamelNotation,
_allDataTypes.Select(datatype => new XElement(_singularCamelNotation,
new XElement(datatype.CamelCaseNotation, datatype.GetDummyData())
))
)
}
);
Soluzione
Anche senza LINQ, non dovresti scrivere xml tramite concatenazione ... XmlWriter
sarebbe una buona scelta:
XmlWriterSettings settings = new XmlWriterSettings();
settings.NewLineHandling = NewLineHandling.Entitize;
settings.Indent = true;
settings.IndentChars = "\t";
StringBuilder sb = new StringBuilder();
using (XmlWriter xw = XmlWriter.Create(sb, settings))
{
xw.WriteStartDocument();
xw.WriteStartElement(_pluralCamelNotation);
for (int i = 0; i < 3; i++)
{
xw.WriteStartElement(_singularCamelNotation);
foreach (DataType dataType in _allDataTypes)
{
xw.WriteElementString(dataType.ToString(),
dataType.GetDummyData());
}
xw.WriteEndElement();
}
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Close();
}
Puoi usare XmlWriterSettings
per controllare cose come l'interlinea.
In alternativa, con LINQ-to-XML:
XDocument doc = new XDocument(
new XDeclaration("1.0", null, null),
new XElement(_pluralCamelNotation,
Enumerable.Range(1,3).Select(
i => new XElement(_singularCamelNotation,
_allDataTypes.Select(
dataType => new XElement(
dataType.ToString(),
dataType.GetDummyData())
)
))));
string t = doc.ToString();
Altri suggerimenti
Questa è un'area che VB.Net vince su C # con l'uso di letterali XML. Guarda questo codice tratto da http://blogs.msdn.com/jimoneil/archive/2009/06/15/x-is-for-xml-literal.aspx . Non è così bello?
Dim xml = <?xml version="1.0"?>
<menu>
<course name="appetizer">
<%= From m In menu _
Where m.Course = "appetizer" _
Select <dish><%= m.Food %></dish> _
%>
</course>
<course name="main">
<%= From m In menu _
Where m.Course = "main" _
Select <dish><%= m.Food %></dish> _
%>
</course>
<course name="dessert">
<%= From m In menu _
Where m.Course = "dessert" _
Select <dish><%= m.Food %></dish> _
%>
</course>
</menu>
Se intendi questa riga:
_allDataTypes.Select(datatype => new XElement(_singularCamelNotation,
new XElement(datatype.CamelCaseNotation, datatype.GetDummyData())
))
e _allDataTypes è un elenco (attenzione non è LinQ!) puoi farlo:
_allDataTypes.ForEach(datatype => new XElement(_singularCamelNotation,
new XElement(datatype.CamelCaseNotation, datatype.GetDummyData())
))
tieni presente che funziona davvero solo con Elenco.