我怎样才能改变这种StringBuilder的到XML的代码,LINQ到XML? [英] How can I change this StringBuilder-to-XML code to LINQ-to-XML?
本文介绍了我怎样才能改变这种StringBuilder的到XML的代码,LINQ到XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序生成具有此代码中使用的XML文件的的StringBuilder
StringBuilder的SB =新的StringBuilder();
sb.Append(< XML版本= \1.0\编码= \UTF-8\>+ Environment.NewLine);
sb.Append(的String.Format(< {0}> {1},_pluralCamelNotation,Environment.NewLine));
为(INT指数= 0;指数3;;指数++)
{
sb.Append(的String.Format(\t< {0}> {1}, _singularCamelNotation,Environment.NewLine));
的foreach(在_allDataTypes数据类型的dataType)
{
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));
返回sb.ToString();
我怎么可以做同样的事情 LINQ ,这样的事情:
伪代码:
变种XDOC =新的XDocument(
新XDeclaration( 1.0,UTF-8,NULL),
为(INT指数= 0;指数3;;指数++){
新的XElement(_pluralCamelNotation,
_allDataTypes.Select(数据类型= >新建的XElement(_singularCamelNotation,
新的XElement(datatype.CamelCaseNotation,datatype.GetDummyData())
))
)
}
);
解决方案
即使没有LINQ,你不应该通过编写XML级联... 的XmlWriter
将是一个不错的选择:
XmlWriterSettings设置=新XmlWriterSettings();
settings.NewLineHandling = NewLineHandling.Entitize;
settings.Indent = TRUE;
settings.IndentChars =\t;
StringBuilder的SB =新的StringBuilder();
使用(XmlWriter的XW = XmlWriter.Create(SB,设置))
{
xw.WriteStartDocument();
xw.WriteStartElement(_pluralCamelNotation);
的for(int i = 0;我3;;我++)
{
xw.WriteStartElement(_singularCamelNotation);
的foreach(在_allDataTypes数据类型的dataType)
{
xw.WriteElementString(dataType.ToString(),
dataType.GetDummyData());
}
xw.WriteEndElement();
}
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Close();
}
您可以使用 XmlWriterSettings
控制之类的东西行距
另外,使用LINQ到XML:
的XDocument DOC =新的XDocument(
新XDeclaration(1.0,NULL,NULL),
新的XElement(_pluralCamelNotation,
Enumerable.Range(1,3)。选择(
I =>新建的XElement(_singularCamelNotation,
_allDataTypes.Select(
的dataType =>新建的XElement(
dataType.ToString(),
dataType.GetDummyData ())
)
))));
串T = doc.ToString();
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())
))
)
}
);
解决方案
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();
这篇关于我怎样才能改变这种StringBuilder的到XML的代码,LINQ到XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文