从 XML 获取 C# 对象的通用列表工作正常,除非我把它放在对象列表中
Getting Generic List of C# objects from XML working fine, except when I put that in a List of Objects
这是可行的:(每个发票都有很多行项目)
public class Invoice
{
public int InvoiceId { get; set; }
public List<LineItem> LineItems { get; set; }
}
public class LineItem
{
public int LineItemId { get; set; }
}
string strInvoice = "<Invoice> <InvoiceId>12345</InvoiceId> <LineItems> <LineItem> <LineItemId>123</LineItemId> </LineItem> <LineItem> <LineItemId>456</LineItemId> </LineItem> </LineItems></Invoice>";
object result = new System.Xml.Serialization.XmlSerializer(typeof(Invoice)).Deserialize(new System.IO.StringReader(strInvoice));
对象被完美反序列化。
但是现在,我尝试制作一个发票清单(我现在将当前 XML 字符串包含在 <Invoices></Invoices>
中):
public class InvoiceList
{
public List<Invoice> Invoices { get; set; }
}
string strInvoices = "<Invoices><Invoice> <InvoiceId>12345</InvoiceId> <LineItems> <LineItem> <LineItemId>123</LineItemId> </LineItem> <LineItem> <LineItemId>456</LineItemId> </LineItem> </LineItems></Invoice></Invoices>";
object results = new System.Xml.Serialization.XmlSerializer(typeof(Invoice)).Deserialize(new System.IO.StringReader(strInvoices));
导致:
There is an error in XML document (1, 2).
{"<Invoices xmlns=''> was not expected."}
如何构造我的 class 或 SQL 以反序列化发票列表?
尝试这个 - 这是从标记为重复的 Twitter 问题的答案中获取的,我没有得到结果:
string strInvoices = "<Invoices><Invoice> <InvoiceId>12345</InvoiceId> <LineItems> <LineItem> <LineItemId>123</LineItemId> </LineItem> <LineItem> <LineItemId>456</LineItemId> </LineItem> </LineItems></Invoice></Invoices>";
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Invoices";
xRoot.IsNullable = true;
object results = new System.Xml.Serialization.XmlSerializer(typeof(Invoice),xRoot).Deserialize(new System.IO.StringReader(strInvoices));
如果我这样做,我会得到同样的错误:
object results = new System.Xml.Serialization.XmlSerializer(typeof(Invoice),xRoot).Deserialize(new System.IO.StringReader(strInvoices));
如果我按照评论中的建议执行此操作,则不会返回任何内容:
object results = new System.Xml.Serialization.XmlSerializer(typeof(InvoiceList),xRoot).Deserialize(new System.IO.StringReader(strInvoices));
因此与标记为重复的示例不同
我付出了很多努力来复制此错误,并使其尽可能通用且易于阅读并可复制并粘贴到 Visual Studio
您正在反序列化为错误的类型 - 您想要反序列化为 InvoiceList
。
您还应该向 InvoiceList
class 添加一些属性以正确映射元素名称:
[XmlRoot("Invoices")]
public class InvoiceList
{
[XmlElement("Invoice")]
public List<Invoice> Invoices { get; set; }
}
然后你应该能够像这样反序列化:
var serializer = new XmlSerializer(typeof(InvoiceList));
using (var reader = new StringReader(strInvoices))
{
var list = (InvoiceList)serializer.Deserialize(reader);
}
有关工作示例,请参阅 https://dotnetfiddle.net/mBtPSP。
这是可行的:(每个发票都有很多行项目)
public class Invoice
{
public int InvoiceId { get; set; }
public List<LineItem> LineItems { get; set; }
}
public class LineItem
{
public int LineItemId { get; set; }
}
string strInvoice = "<Invoice> <InvoiceId>12345</InvoiceId> <LineItems> <LineItem> <LineItemId>123</LineItemId> </LineItem> <LineItem> <LineItemId>456</LineItemId> </LineItem> </LineItems></Invoice>";
object result = new System.Xml.Serialization.XmlSerializer(typeof(Invoice)).Deserialize(new System.IO.StringReader(strInvoice));
对象被完美反序列化。
但是现在,我尝试制作一个发票清单(我现在将当前 XML 字符串包含在 <Invoices></Invoices>
中):
public class InvoiceList
{
public List<Invoice> Invoices { get; set; }
}
string strInvoices = "<Invoices><Invoice> <InvoiceId>12345</InvoiceId> <LineItems> <LineItem> <LineItemId>123</LineItemId> </LineItem> <LineItem> <LineItemId>456</LineItemId> </LineItem> </LineItems></Invoice></Invoices>";
object results = new System.Xml.Serialization.XmlSerializer(typeof(Invoice)).Deserialize(new System.IO.StringReader(strInvoices));
导致:
There is an error in XML document (1, 2).
{"<Invoices xmlns=''> was not expected."}
如何构造我的 class 或 SQL 以反序列化发票列表?
尝试这个 - 这是从标记为重复的 Twitter 问题的答案中获取的,我没有得到结果:
string strInvoices = "<Invoices><Invoice> <InvoiceId>12345</InvoiceId> <LineItems> <LineItem> <LineItemId>123</LineItemId> </LineItem> <LineItem> <LineItemId>456</LineItemId> </LineItem> </LineItems></Invoice></Invoices>";
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Invoices";
xRoot.IsNullable = true;
object results = new System.Xml.Serialization.XmlSerializer(typeof(Invoice),xRoot).Deserialize(new System.IO.StringReader(strInvoices));
如果我这样做,我会得到同样的错误:
object results = new System.Xml.Serialization.XmlSerializer(typeof(Invoice),xRoot).Deserialize(new System.IO.StringReader(strInvoices));
如果我按照评论中的建议执行此操作,则不会返回任何内容:
object results = new System.Xml.Serialization.XmlSerializer(typeof(InvoiceList),xRoot).Deserialize(new System.IO.StringReader(strInvoices));
因此与标记为重复的示例不同 我付出了很多努力来复制此错误,并使其尽可能通用且易于阅读并可复制并粘贴到 Visual Studio
您正在反序列化为错误的类型 - 您想要反序列化为 InvoiceList
。
您还应该向 InvoiceList
class 添加一些属性以正确映射元素名称:
[XmlRoot("Invoices")]
public class InvoiceList
{
[XmlElement("Invoice")]
public List<Invoice> Invoices { get; set; }
}
然后你应该能够像这样反序列化:
var serializer = new XmlSerializer(typeof(InvoiceList));
using (var reader = new StringReader(strInvoices))
{
var list = (InvoiceList)serializer.Deserialize(reader);
}
有关工作示例,请参阅 https://dotnetfiddle.net/mBtPSP。