How can I change this StringBuilder-to-XML code to LINQ-to-XML?

馋奶兔 提交于 2019-12-22 00:34:13

问题


In my application I build an XML file with this code using 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();

How can I do the same thing with LINQ, something like this:

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())
            ))
        )
    }
);

回答1:


Even without LINQ, you shouldn't write xml via concatenation... XmlWriter would be a good fit:

    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();
    }

You can use XmlWriterSettings to control things like line spacing.

Alternatively, with 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();



回答2:


This is one area that VB.Net wins over C# with it use of XML literals. Look at this code taken from http://blogs.msdn.com/jimoneil/archive/2009/06/15/x-is-for-xml-literal.aspx. Ain't that beautiful?

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>



回答3:


If you mean this row :

_allDataTypes.Select(datatype => new XElement(_singularCamelNotation,
                new XElement(datatype.CamelCaseNotation, datatype.GetDummyData())
            ))

and _allDataTypes is a List (attention is no LinQ!)you can do this:

_allDataTypes.ForEach(datatype => new XElement(_singularCamelNotation,
                new XElement(datatype.CamelCaseNotation, datatype.GetDummyData())
            ))

be aware this really works only with List.



来源:https://stackoverflow.com/questions/1032314/how-can-i-change-this-stringbuilder-to-xml-code-to-linq-to-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!