It seems that you must execute memoryStream.Position = 0 between attempts.
System.InvalidOperationException: There is an error in XML document (0, 0). ---> System.Xml.XmlException: Root element is missing.
Hi, Everybody!...
I am trying to parse the following XML string...
<PostShipmentResponse>
<Account>L303061</Account>
<ParcelhubShipmentId>2815849350974933</ParcelhubShipmentId>
<Reference1>ordernumber1</Reference1>
<Reference2>label reference</Reference2>
<SpecialInstructions>leave in shed</SpecialInstructions>
<ContentsDescription>overview of what's being sent</ContentsDescription>
<Blobs />
<ModifiedTime>2023-09-27T18:20:59.9200581+01:00</ModifiedTime>
<CurrencyCode>GBP</CurrencyCode>
<Deleted>false</Deleted>
<HasBeenManifested>false</HasBeenManifested>
<Department>L3030611</Department>
<Attributes />
<Returnable>false</Returnable>
</PostShipmentResponse>
I applied the following XML attributes...
public partial class PostShipmentResponse
{
//[XmlElement(ElementName = "Shipment")]
//public Shipment Shipment { get; set; }
[XmlElement(ElementName = "Account")]
public string Account { get; set; }
//[XmlElement(ElementName = "ServiceInfo")]
//public ServiceInfo ServiceInfo { get; set; }
[XmlElement(ElementName = "ParcelhubShipmentId")]
public string ParcelhubShipmentId { get; set; }
//[XmlElement(ElementName = "ShippingInfo")]
//public ShippingInfo ShippingInfo { get; set; }
//[XmlElement(ElementName = "CollectionDetails")]
//public CollectionDetails CollectionDetails { get; set; }
//[XmlElement(ElementName = "CollectionAddress")]
//public CollectionAddress CollectionAddress { get; set; }
//[XmlElement(ElementName = "DeliveryAddress")]
//public DeliveryAddress DeliveryAddress { get; set; }
//[XmlElement(ElementName = "DeliverToStoreOrLockerID")]
//public string DeliverToStoreOrLockerID { get; set; }
[XmlElement(ElementName = "Reference1")]
public string Reference1 { get; set; }
[XmlElement(ElementName = "Reference2")]
public string Reference2 { get; set; }
[XmlElement(ElementName = "SpecialInstructions")]
public string SpecialInstructions { get; set; }
[XmlElement(ElementName = "ContentsDescription")]
public string ContentsDescription { get; set; }
//[XmlElement(ElementName = "Packages")]
//public Packages Packages { get; set; }
//[XmlElement(ElementName = "Enhancements")]
//public Enhancements Enhancements { get; set; }
[XmlElement(ElementName = "Blobs")]
public string Blobs { get; set; }
[XmlElement(ElementName = "ModifiedTime")]
public string ModifiedTime { get; set; }
[XmlElement(ElementName = "CurrencyCode")]
public string CurrencyCode { get; set; }
//[XmlElement(ElementName = "CustomsDeclarationInfo")]
//public CustomsDeclarationInfo CustomsDeclarationInfo { get; set; }
[XmlElement(ElementName = "Deleted")]
public string Deleted { get; set; }
[XmlElement(ElementName = "HasBeenManifested")]
public string HasBeenManifested { get; set; }
//[XmlElement(ElementName = "ShipmentTags")]
//public ShipmentTags ShipmentTags { get; set; }
//[XmlElement(ElementName = "Stream")]
//public string Stream { get; set; }
[XmlElement(ElementName = "Department")]
public string Department { get; set; }
[XmlElement(ElementName = "Attributes")]
public string Attributes { get; set; }
[XmlElement(ElementName = "Returnable")]
public string Returnable { get; set; }
//[XmlElement(ElementName = "NominatedDeliveryDate")]
//public NominatedDeliveryDate NominatedDeliveryDate { get; set; }
//[XmlElement(ElementName = "ProductTypeCode")]
//public string ProductTypeCode { get; set; }
//[XmlAttribute(AttributeName = "xsd")]
//public string Xsd { get; set; }
//[XmlAttribute(AttributeName = "xsi")]
//public string Xsi { get; set; }
//[XmlAttribute(AttributeName = "xmlns")]
//public string Xmlns { get; set; }
//[XmlText]
//public string Text { get; set; }
}
I'm executing the following code...
private TResponse XmlStringToObject<TResponse>(string xmlString)
{
using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
{
XmlSerializer xmlSerializer;
xmlSerializer = new XmlSerializer(typeof(TResponse));
return (TResponse)xmlSerializer.Deserialize(memoryStream);
}
}
I'm getting the followig error...
System.InvalidOperationException: There is an error in XML document (0, 0). ---> System.Xml.XmlException: Root element is missing.
What am I going wrong?
Developer technologies .NET .NET Runtime
Developer technologies .NET Other
Developer technologies ASP.NET Other
Developer technologies C#
1 additional answer
Sort by: Most helpful
-
Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
2023-09-28T02:57:15.6266667+00:00 Hi @Julio Bello,
I tested the code you provided and there is no problem. You didn't provide a method to read the xml, maybe that's a problem.
You can add the following content to your XML and see if an error is reported.
<?xml version="1.0" encoding="utf-8"?> <rootElement> ... </rootElement>
Or try using my test method, here is the complete test code:
public class Program { public static void Main(string[] args) { Serializer ser = new Serializer(); string path = string.Empty; string xmlInputData = string.Empty; xmlInputData = File.ReadAllText(@"C:\Users\Administrator\Desktop\test.xml"); var customer = ser.XmlStringToObject<PostShipmentResponse>(xmlInputData); Console.WriteLine(customer); } public class Serializer { public TResponse XmlStringToObject<TResponse>(string xmlString) { using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString))) { XmlSerializer xmlSerializer; xmlSerializer = new XmlSerializer(typeof(TResponse)); return (TResponse)xmlSerializer.Deserialize(memoryStream); } } } public partial class PostShipmentResponse { //[XmlElement(ElementName = "Shipment")] //public Shipment Shipment { get; set; } [XmlElement(ElementName = "Account")] public string Account { get; set; } //[XmlElement(ElementName = "ServiceInfo")] //public ServiceInfo ServiceInfo { get; set; } [XmlElement(ElementName = "ParcelhubShipmentId")] public string ParcelhubShipmentId { get; set; } //[XmlElement(ElementName = "ShippingInfo")] //public ShippingInfo ShippingInfo { get; set; } //[XmlElement(ElementName = "CollectionDetails")] //public CollectionDetails CollectionDetails { get; set; } //[XmlElement(ElementName = "CollectionAddress")] //public CollectionAddress CollectionAddress { get; set; } //[XmlElement(ElementName = "DeliveryAddress")] //public DeliveryAddress DeliveryAddress { get; set; } //[XmlElement(ElementName = "DeliverToStoreOrLockerID")] //public string DeliverToStoreOrLockerID { get; set; } [XmlElement(ElementName = "Reference1")] public string Reference1 { get; set; } [XmlElement(ElementName = "Reference2")] public string Reference2 { get; set; } [XmlElement(ElementName = "SpecialInstructions")] public string SpecialInstructions { get; set; } [XmlElement(ElementName = "ContentsDescription")] public string ContentsDescription { get; set; } //[XmlElement(ElementName = "Packages")] //public Packages Packages { get; set; } //[XmlElement(ElementName = "Enhancements")] //public Enhancements Enhancements { get; set; } [XmlElement(ElementName = "Blobs")] public string Blobs { get; set; } [XmlElement(ElementName = "ModifiedTime")] public string ModifiedTime { get; set; } [XmlElement(ElementName = "CurrencyCode")] public string CurrencyCode { get; set; } //[XmlElement(ElementName = "CustomsDeclarationInfo")] //public CustomsDeclarationInfo CustomsDeclarationInfo { get; set; } [XmlElement(ElementName = "Deleted")] public string Deleted { get; set; } [XmlElement(ElementName = "HasBeenManifested")] public string HasBeenManifested { get; set; } //[XmlElement(ElementName = "ShipmentTags")] //public ShipmentTags ShipmentTags { get; set; } //[XmlElement(ElementName = "Stream")] //public string Stream { get; set; } [XmlElement(ElementName = "Department")] public string Department { get; set; } [XmlElement(ElementName = "Attributes")] public string Attributes { get; set; } [XmlElement(ElementName = "Returnable")] public string Returnable { get; set; } //[XmlElement(ElementName = "NominatedDeliveryDate")] //public NominatedDeliveryDate NominatedDeliveryDate { get; set; } //[XmlElement(ElementName = "ProductTypeCode")] //public string ProductTypeCode { get; set; } //[XmlAttribute(AttributeName = "xsd")] //public string Xsd { get; set; } //[XmlAttribute(AttributeName = "xsi")] //public string Xsi { get; set; } //[XmlAttribute(AttributeName = "xmlns")] //public string Xmlns { get; set; } //[XmlText] //public string Text { get; set; } } }
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.