我正在尝试连接两个字符串构建器,每个构建器都包含XML。
当我单独添加details
时它工作得很好..与 public string XMLDoc(SalesForecastModel model)
{
string doc = string.Empty;
StringBuilder builder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(builder))
{
writer.WriteStartDocument();
writer.WriteStartElement("header");
writer.WriteStartElement("SalesForecast");
writer.WriteStartElement("EntryNumber");
writer.WriteString(model.EntryNumber.ToString());
writer.WriteEndElement();
writer.WriteStartElement("EntryDate");
writer.WriteString(model.EntryDate.ToString());
writer.WriteEndElement();
writer.WriteStartElement("Year");
writer.WriteString(model.Year.ToString());
writer.WriteEndElement();
writer.WriteStartElement("Remarks");
writer.WriteString(model.Remarks.ToString());
writer.WriteEndElement();
writer.WriteStartElement("RevisionID");
writer.WriteString(model.RevisionID.ToString());
writer.WriteEndElement();
writer.WriteStartElement("Status");
writer.WriteString(model.Status.ToString());
writer.WriteEndElement();
writer.WriteStartElement("CreatedBy");
writer.WriteString(model.CreatedBy.ToString());
writer.WriteEndElement();
writer.WriteStartElement("CreatedDate");
writer.WriteString(model.CreatedDate.ToString());
writer.WriteEndElement();
writer.WriteStartElement("LastModifiedBy");
writer.WriteString(model.LastModifiedBy.ToString());
writer.WriteEndElement();
writer.WriteStartElement("LastModifiedDate");
writer.WriteString(model.LastModifiedDate.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}
StringBuilder builder1 = new StringBuilder();
using (XmlWriter writer1 = XmlWriter.Create(builder1))
{
writer1.WriteStartDocument();
writer1.WriteStartElement("details");
foreach (SalesForecastDetailModel detail in model.Details)
{
writer1.WriteStartElement("SalesForecastDetails");
writer1.WriteStartElement("MonthID");
writer1.WriteString(detail.MonthID.ToString());
writer1.WriteEndElement();
writer1.WriteEndElement();
}
writer1.WriteEndElement();
writer1.WriteEndDocument();
}
return doc = builder.Append(builder1).ToString();
}
相同但是当我追加它们并保存到数据库时它会给我一个错误..
这是错误:
{“XML解析:第1行,第418位,text / xmldecl不在输入开头”}
{{1}}
答案 0 :(得分:1)
使用XmlWriter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication78
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string doc = string.Empty;
StringBuilder builder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(builder))
{
XElement salesForecast =
new XElement(
"SalesForecast",
new XElement(
"header",
new XElement("EntryNumber", model.EntryNumber),
new XElement("EntryDate", model.EntryDate),
new XElement("Year", model.Year),
new XElement("Remarks", model.Remarks),
new XElement("RevisionID", model.RevisionID),
new XElement("Status", model.Status),
new XElement("CreatedBy", model.CreatedBy),
new XElement("CreatedDate", model.CreatedDate),
new XElement("LastModifiedBy", model.LastModifiedBy),
new XElement("LastModifiedDate", model.LastModifiedDate)),
new XElement(
"details",
model.Details.Select(detail =>
new XElement(
"SalesForecastDetails",
new XElement("MonthID", detail.MonthID)))));
writer.WriteNode(salesForecast);
writer.WriteEndDocument(); //closes any open tags
}
}
}
}
答案 1 :(得分:-1)
您正在尝试将两个有效的XML文档连接在一起。这不会创建有效的XML文档。
就好像你有<a>x</a>
(有效的XML)&amp; <b>y</b>
(有效的XML)然后加入了<a>x</a><b>y</b>
(无效 XML)。
您需要使用新的父节点包围它们或将其中的一个嵌套在另一个父节点中。我将假设一个新的父节点。
现在,考虑到结构可以移动,我认为有一个非常好的输出格式。我还将使用System.Xml.Linq
,因为它可以生成更容易阅读的代码。
试试这个:
public string XMLDoc(SalesForecastModel model)
{
var xml =
new XElement(
"SalesForecast",
new XElement(
"header",
new XElement("EntryNumber", model.EntryNumber),
new XElement("EntryDate", model.EntryDate),
new XElement("Year", model.Year),
new XElement("Remarks", model.Remarks),
new XElement("RevisionID", model.RevisionID),
new XElement("Status", model.Status),
new XElement("CreatedBy", model.CreatedBy),
new XElement("CreatedDate", model.CreatedDate),
new XElement("LastModifiedBy", model.LastModifiedBy),
new XElement("LastModifiedDate", model.LastModifiedDate)),
new XElement(
"details",
model.Details.Select(detail =>
new XElement(
"SalesForecastDetails",
new XElement("MonthID", detail.MonthID)))));
return xml.ToString();
}