C# 如何将两个XML字符串连接成单个有效XML

C# 如何将两个XML字符串连接成单个有效XML,c#,xml,stringbuilder,C#,Xml,Stringbuilder,我试图连接两个字符串生成器,每个都包含XML。 当我单独添加标题时,它可以正常工作。。与详细信息相同但当我附加。。。然后保存到数据库中,这会给我一个错误 以下是错误: {“XML解析:第1行,字符418,text/xmldecl不在输入的开头”} 您正在尝试将两个有效的XML文档连接在一起。这不会创建有效的XML文档 这就像你有x(有效的XML)和y(有效的XML),然后像xy(无效的XML)一样加入它们 您需要用一个新的父节点包围它们,或者将一个节点嵌套在另一个节点中。我将假设一个新的父节点

我试图连接两个字符串生成器,每个都包含XML。 当我单独添加
标题时,它可以正常工作。。与
详细信息相同
但当我附加。。。然后保存到数据库中,这会给我一个错误

以下是错误:

{“XML解析:第1行,字符418,text/xmldecl不在输入的开头”}


您正在尝试将两个有效的XML文档连接在一起。这不会创建有效的XML文档

这就像你有
x
(有效的XML)和
y
(有效的XML),然后像
xy
无效的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();
}

您正在尝试将两个有效的XML文档连接在一起。这不会创建有效的XML文档

这就像你有
x
(有效的XML)和
y
(有效的XML),然后像
xy
无效的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();
}
使用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
            }

        }
    }
}
使用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
            }

        }
    }
}

95%的答案是肯定的。你可以计算出剩下的部分,然后发布自我回答(或者作为副本进行投票)。你不需要连接两个StringBuilder。您正在尝试合并两个恰好由StringBuilder包含的XML文档。这是一个与你所问的完全不同的问题,我很确定以前在这里也有人问过这个问题。您可以尝试搜索合并XML内容或插入XML节点。95%的答案是。你可以计算出剩下的部分,然后发布自我回答(或者作为副本进行投票)。你不需要连接两个StringBuilder。您正在尝试合并两个恰好由StringBuilder包含的XML文档。这是一个与你所问的完全不同的问题,我很确定以前在这里也有人问过这个问题。您可以尝试搜索合并XML内容或插入XML节点。