I need the following output
<BatchFile>
<BatchFileHeader>
<MessageType>MSG103</MessageType>
<Version>456</Version>
<BatchFileHeader>
<Vendor>
<VendorType>VVV</VendorType>
</Vendor>
</BatchFile>
Following is my code
StringWriter stringwriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringwriter);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("BatchFileHeader");
xmlTextWriter.WriteElementString("MessageType", "MSG103");
xmlTextWriter.WriteElementString("MessageVersion", "456");
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteStartElement("Vendor");
xmlTextWriter.WriteElementString("Vendor", "VVV");
xmlTextWriter.WriteEndElement();
But I get the following error
token startelement in state epilog would result in an invalid xml document
What am I doing wrong ?
You forgot to write the outermost StartElement <BatchElement>
.
Adding he following should fix your problem;
StringWriter stringwriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringwriter);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("BatchElement");
xmlTextWriter.WriteStartElement("BatchFileHeader");
xmlTextWriter.WriteElementString("MessageType", "MSG103");
xmlTextWriter.WriteElementString("MessageVersion", "456");
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteStartElement("Vendor");
xmlTextWriter.WriteElementString("Vendor", "VVV");
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.