错误:{"Token StartElement in state End Document would result in an invalid XML document."}
当前在阅读第二个客户端时被writer.WriteStartElement(reader.Name);
行抛出
期望的结果:我需要读取XML,最顶层的节点对于每个收件人节点及其关联的后续节点(消息和详细信息)将是通用的。所以基本上我需要为每个收件人创建一个XML及其后续的Message和Detail元素。
顶级的常见XML,
<!--Data Design : \Data\GenericLetter\Data\DSIletter-->
<!--Input Design : CSF Standard XML-->
<FILE NAME="DSIletter" version="14.0.0.112"
.....
我可以很好地阅读第一个客户端但是在尝试阅读第二个客户端时我得到了上面提到的错误...
C#:
XmlTextReader reader = new XmlTextReader(@"C:\Users\GRPAdmin\Desktop\DSIGenericLetterBatch.xml");
string thisClientXML = "";
string sXMLStartElement = "";
StringBuilder output = new StringBuilder();
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
ws.OmitXmlDeclaration = true;
ws.CheckCharacters = false;
XmlWriter writer = XmlWriter.Create(output, ws);
XmlWriter writerStart = null;
bool FirstClient = true;
while (reader.Read())
{
int lineNumber = reader.LineNumber;
if (reader.HasAttributes && reader.GetAttribute(0) == "Recipient")
{
Console.WriteLine("NEW CLIENT");
if (FirstClient)
{
FirstClient = false;
sXMLStartElement = output.ToString() + ">";
writerStart = writer;
}
else
{
writer.WriteEndDocument();
writer.Flush();
thisClientXML = output.ToString();
output.Clear();
output.Length = 0;
//output.Append(sXMLStartElement.Remove(sXMLStartElement.Length - 1, 1));
output.Append(sXMLStartElement);
//writer.Close();
writer = null;
// writer = XmlWriter.Create(output, ws);
writer = writerStart;
}
}
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Name);
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
writer.Flush();
if (reader.HasAttributes)
{
while (reader.MoveToNextAttribute())
{
writer.WriteAttributeString(reader.Name, reader.Value);
}
}
}
XML:
<!--Data Design : \Data\GenericLetter\Data\DSIletter-->
<!--Input Design : CSF Standard XML-->
<FILE NAME="DSIletter" version="14.0.0.112" State="WIP" Template="DSIGenericLetter V2" origin="designer" __IntuitionUser="admin" __InputID="8f5599ef-fe08-48ea-9b44-075a95c5cfbf">
<REC NAME="Recipient">
<FLD NAME="FirstName">James</FLD>
<FLD NAME="LastName">McLeman</FLD>
<FLD NAME="Company">DSI</FLD>
<FLD NAME="Reference">2131231243257657</FLD>
<FLD NAME="JobDescription">Director</FLD>
<FLD NAME="Address1">5 Fintry Court</FLD>
<FLD NAME="Address2">Kellyville</FLD>
<FLD NAME="Address3">NSW</FLD>
<FLD NAME="PostCode">2155</FLD>
<FLD NAME="Brand">DSI</FLD>
<FLD NAME="Amount">999.99</FLD>
<FLD NAME="FreeFormText">Have a great day</FLD>
<FLD NAME="Text1">xxx</FLD>
<FLD NAME="Text2">xxx</FLD>
<FLD NAME="Text3">xxx</FLD>
<FLD NAME="Number1">0</FLD>
<FLD NAME="Number2">0</FLD>
<FLD NAME="Number3">0</FLD>
<FLD NAME="MarketingMessageKey">Boat</FLD>
<FLD NAME="EditState">DocMode</FLD>
<FLD NAME="Panel">X</FLD>
<FLD NAME="Text">X</FLD>
<FLD NAME="EmailAddress">X</FLD>
<FLD NAME="Mobile">X</FLD>
<FLD NAME="Balance">X</FLD>
<FLD NAME="BirthDate">01/01/1980</FLD>
<FLD NAME="MaritalStatus">X</FLD>
<FLD NAME="DueDate">20130705</FLD>
<FLD NAME="Excess">1000</FLD>
<FLD NAME="Subject">X</FLD>
<FLD NAME="Gender">Male</FLD>
</REC>
<REC NAME="Message">
<FLD NAME="Key">Introduction</FLD>
<FLD NAME="Text1">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</FLD>
<FLD NAME="Text2">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</FLD>
<FLD NAME="Text3">X</FLD>
<FLD NAME="Text4">X</FLD>
<FLD NAME="Number1">999999999999999999.99</FLD>
<FLD NAME="Number2">999999999999999999.99</FLD>
<FLD NAME="Number3">X</FLD>
<FLD NAME="Number4">X</FLD>
<FLD NAME="FreeText">---</FLD>
</REC>
<REC NAME="Details">
<FLD NAME="Key">default</FLD>
<FLD NAME="Description">description</FLD>
<FLD NAME="Number1">0</FLD>
<FLD NAME="Number2">0</FLD>
<FLD NAME="Text1"> xxx</FLD>
<FLD NAME="Text2"> xxx</FLD>
<FLD NAME="FreeFormText"> xxx</FLD>
</REC>
<FLD NAME="Key">default</FLD>
<FLD NAME="Description">support</FLD>
<FLD NAME="Number1">0</FLD>
<FLD NAME="Number2">0</FLD>
<FLD NAME="Text1"> xxx</FLD>
<FLD NAME="Text2"> xxx</FLD>
<FLD NAME="FreeFormText"> xxx</FLD>
</REC>
<REC NAME="Recipient">
<FLD NAME="FirstName">Fred</FLD>
<FLD NAME="LastName">McLeman</FLD>
<FLD NAME="Company">DSI</FLD>
<FLD NAME="Reference">2131231243257657</FLD>
<FLD NAME="JobDescription">Director</FLD>
<FLD NAME="Address1">5 Fintry Court</FLD>
<FLD NAME="Address2">Kellyville</FLD>
<FLD NAME="Address3">NSW</FLD>
<FLD NAME="PostCode">2155</FLD>
<FLD NAME="Brand">DSI</FLD>
<FLD NAME="Amount">999.99</FLD>
<FLD NAME="FreeFormText">Have a great day</FLD>
<FLD NAME="Text1">xxx</FLD>
<FLD NAME="Text2">xxx</FLD>
<FLD NAME="Text3">xxx</FLD>
<FLD NAME="Number1">0</FLD>
<FLD NAME="Number2">0</FLD>
<FLD NAME="Number3">0</FLD>
<FLD NAME="MarketingMessageKey">Boat</FLD>
<FLD NAME="EditState">DocMode</FLD>
<FLD NAME="Panel">X</FLD>
<FLD NAME="Text">X</FLD>
<FLD NAME="EmailAddress">X</FLD>
<FLD NAME="Mobile">X</FLD>
<FLD NAME="Balance">X</FLD>
<FLD NAME="BirthDate">01/01/1980</FLD>
<FLD NAME="MaritalStatus">X</FLD>
<FLD NAME="DueDate">20130705</FLD>
<FLD NAME="Excess">1000</FLD>
<FLD NAME="Subject">X</FLD>
<FLD NAME="Gender">Male</FLD>
</REC>
<REC NAME="Message">
<FLD NAME="Key">Introduction</FLD>
<FLD NAME="Text1">Our core services</FLD>
<FLD NAME="Text2">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</FLD>
<FLD NAME="Text3">X</FLD>
<FLD NAME="Text4">X</FLD>
<FLD NAME="Number1">999999999999999999.99</FLD>
<FLD NAME="Number2">999999999999999999.99</FLD>
<FLD NAME="Number3">X</FLD>
<FLD NAME="Number4">X</FLD>
<FLD NAME="FreeText">---</FLD>
</REC>
<REC NAME="Details">
<FLD NAME="Key">default</FLD>
<FLD NAME="Description">description</FLD>
<FLD NAME="Number1">0</FLD>
<FLD NAME="Number2">0</FLD>
<FLD NAME="Text1"> xxx</FLD>
<FLD NAME="Text2"> xxx</FLD>
<FLD NAME="FreeFormText"> xxx</FLD>
</REC>
<REC NAME="Recipient">
<FLD NAME="FirstName">James</FLD>
<FLD NAME="LastName">McLeman</FLD>
<FLD NAME="Company">DSI</FLD>
<FLD NAME="Reference">2131231243257657</FLD>
<FLD NAME="JobDescription">Director</FLD>
<FLD NAME="Address1">5 Fintry Court</FLD>
<FLD NAME="Address2">Kellyville</FLD>
<FLD NAME="Address3">NSW</FLD>
<FLD NAME="PostCode">2155</FLD>
<FLD NAME="Brand">DSI</FLD>
<FLD NAME="Amount">999.99</FLD>
<FLD NAME="FreeFormText">Have a great time in Bora Bora.</FLD>
<FLD NAME="Text1">xxx</FLD>
<FLD NAME="Text2">xxx</FLD>
<FLD NAME="Text3">xxx</FLD>
<FLD NAME="Number1">0</FLD>
<FLD NAME="Number2">0</FLD>
<FLD NAME="Number3">0</FLD>
<FLD NAME="MarketingMessageKey">Boat</FLD>
<FLD NAME="EditState">DocMode</FLD>
<FLD NAME="Panel">X</FLD>
<FLD NAME="Text">X</FLD>
<FLD NAME="EmailAddress">X</FLD>
<FLD NAME="Mobile">X</FLD>
<FLD NAME="Balance">X</FLD>
<FLD NAME="BirthDate">01/01/1980</FLD>
<FLD NAME="MaritalStatus">X</FLD>
<FLD NAME="DueDate">20130705</FLD>
<FLD NAME="Excess">1000</FLD>
<FLD NAME="Subject">X</FLD>
<FLD NAME="Gender">Male</FLD>
</REC>
<REC NAME="Message">
<FLD NAME="Key">Introduction</FLD>
<FLD NAME="Text1">our fantastic services</FLD>
<FLD NAME="Text2">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</FLD>
<FLD NAME="Text3">X</FLD>
<FLD NAME="Text4">X</FLD>
<FLD NAME="Number1">999999999999999999.99</FLD>
<FLD NAME="Number2">999999999999999999.99</FLD>
<FLD NAME="Number3">X</FLD>
<FLD NAME="Number4">X</FLD>
<FLD NAME="FreeText">---</FLD>
</REC>
<FLD NAME="Key">default</FLD>
<FLD NAME="Description">description</FLD>
<FLD NAME="Number1">0</FLD>
<FLD NAME="Number2">0</FLD>
<FLD NAME="Text1">No GST</FLD>
<FLD NAME="Text2"> xxx</FLD>
<FLD NAME="FreeFormText"> xxx</FLD>
</REC>
</FILE>
答案 0 :(得分:0)
您的问题出在编写器上 - 您正在执行'writer.WriteEndDocument()',然后尝试将另一个启动元素写入编写器,错误告诉您在结束后无法编写任何内容。
看来,'else'中的大部分逻辑(在'if(FirstClient)'之后)应该在主'while'循环之后向下移动到代码的末尾。完成所有读/写后,然后调用writer.Flush()和writer.WriteEndDocument()。