如何将XML解析为自定义类?

时间:2015-01-06 23:59:55

标签: c# linq-to-xml compact-framework xmldocument xmlnodelist

我有包含xml的字符串,我需要循环,解析和构建自定义类的实例化,以便插入到我的数据库中。

我需要的伪代码是这样的:

private List<SiteMapping> ExtractSiteMappingsFromXML(String xmlData)
{
    List<SiteMapping> sitemaps = new List<SiteMapping>();
    // parse xmlData, dynamically instantiating a SiteMapping class for each SiteMapping "record" 
in the xml
    foreach (record rec in xmlData)
    {
        SiteMapping sm = new SiteMapping();
        sm.Id = //current id found in the xml data
        sm.siteName = // current site name found in the xml data
        . . .
        sitemaps.Add(sm);
    }
    return sitemaps;
}

ExtractSiteMappingsFromXML()的调用者将遍历返回的SiteMapping列表,并将记录插入数据库。

基于我从here获得的想法,我认为这样的事情可能是可能的:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlData);
XmlNodeList _ids = doc.GetElementsByTagName("Id");
XmlNodeList _sitenames = doc.GetElementsByTagName("siteName");
. . . // add an XmlNodeList for each element

然后我可以遍历XmlNodeLists,例如:

for (int i = 0; i < _ids.Count; i++)
{
    SiteMapping sm = new SiteMapping();
    sm.Id =_ids[i];
    sm.siteName = _sitenames[i];
    . . . // add the rest
    sitemaps.Add(sm);
}
这是明智的吗?如果一个或多个元素具有空白值,这仍然有用吗? IOW,如果一个元素有时是空白的,它会在相应的XmlNodeList中添加一个空白值(这就是我想要的),还是它什么都不添加,从而造成不匹配?

是否有一种优雅的linqy(LINQ-to-XML)方式呢?

注意:这是一个Compact Framework应用程序,因此受到实施方面的限制。

更新

我想也许这段代码:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(omnivore);
List<SiteQuery> sitequeries =
  (from sitequery in xmlDoc.Descendants("SiteQuery")
   select new SiteQuery
   {
       Id = sitequery.Element("Id").Value,
       UPC_PackSize = sitequery.Element("UPC_PackSize").Value,
       UPC_Code = sitequery.Element("UPC_Code").Value,
   }).ToList<SiteQuery>();

...我改编自here,可以做到这一点,但我明白了,“没有重载方法'后代'需要1个参数

更新2

我试过这个(XDocument而不是XmlDocument):

XDocument xmlDoc = new XDocument();
XDocument.Parse(omnivore);
List<SiteQuery> sitequeries =
 (from sitequery in xmlDoc.Descendants("SiteQuery")
  select new SiteQuery
  {
      Id = Convert.ToInt32(sitequery.Element("Id").Value),
      UPC_PackSize = Convert.ToInt32(sitequery.Element("UPC_PackSize").Value),
      UPC_Code = sitequery.Element("UPC_Code").Value
  }).ToList<SiteQuery>();

我不得不使用“ XDocument.Parse(杂食动物); ”代替“ xmlDoc.Parse(omnivore); ”,但是编译告诉我,那是必要的......?!?

不出所料,在此代码运行后,sitequeries的计数为0,但是......

更新3

也许Nitin Aggarwal的代码可以工作(它确实可以编译),但在运行时我得到:

System.InvalidOperationException was unhandled
  _HResult=-2146233079
  _message=There is an error in XML document (1, 2).
  HResult=-2146233079
  IsTransient=false
  Message=There is an error in XML document (1, 2).
  Source=System.Xml
  StackTrace:
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader). . .

可能只是XML很糟糕;而且,我不知道Compact Framework中是否可以使用这些jet-age类(我已经在.NET 4.5.1测试应用程序中进行了编译)。

更新4

Vishal,回答你的问题,这是我要解析的XML:

<ArrayOfSiteQuery xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS"><SiteQuery><Id>00006000002</Id><UPCPackSize>1</UPCPackSize><UPC_Code>00006000002</UPC_Code><crvId></crvId><dept>8</dept><description>ZZ</description><openQty>0.0</openQty><packSize>1</packSize><subDept>80</subDept><unitCost>1.25</unitCost><unitList>5.0</unitList><vendorId>CONFLICT</vendorId><vendorItem>123456</vendorItem></SiteQuery>
.  . . (beaucoup other SiteQuery "records")
<SiteQuery><Id>5705654</Id><UPCPackSize>1</UPCPackSize><UPC_Code>5705654</UPC_Code><crvId></crvId><dept>2</dept><description>what do you want</description><openQty>0.0</openQty><packSize>1</packSize><subDept>0</subDept><unitCost>0.55</unitCost><unitList>1.62</unitList><vendorId></vendorId><vendorItem></vendorItem></SiteQuery></ArrayOfSiteQuery>

我是否需要先删除初步位(<ArrayOfSiteQuery xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS">)和末尾的“关闭标记”(?)?

BTW,“CStore.DomainModels.HHS”在服务器应用程序中,客户端可能不知道那是什么。

更新5

在查看字符串中的xml之后,我看到它的内容与我的自定义类不匹配(它是相同的数据,但是一些成员名称不同,并且它们彼此无序),所以我更改了自定义类以匹配xml:

public class SiteQuery
{
    public int Id { get; set; }
    public int UPCPackSize { get; set; }
    public String UPC_Code { get; set; }
    public String crvId { get; set; }
    public int dept { get; set; }
    public String description { get; set; }
    public Double openQty { get; set; }
    public int packSize { get; set; }
    public int subDept { get; set; }
    public Decimal unitCost { get; set; }
    public Decimal unitList { get; set; }
    public String vendorId { get; set; }
    public String vendorItem { get; set; }
}

...但我仍然得到相同的InvalidOp异常......

更新6

即使我从xml中删除了序言和后同步码,因此它只包含SiteQuery“xml记录”,将其保存为文件并将其加载以进行处理:

String testData = File.ReadAllText("siteQueryTest.txt");
XmlSerializer serializer = new XmlSerializer(typeof(List<SiteQuery>));
XmlReader reader = XmlReader.Create(new StringReader(testData));
List<SiteQuery> siteQueries;
siteQueries = (List<SiteQuery>)serializer.Deserialize(reader);

...我仍然遇到运行时错误:

System.InvalidOperationException was unhandled
  _HResult=-2146233079
  _message=There is an error in XML document (1, 2).
  HResult=-2146233079
  IsTransient=false
  Message=There is an error in XML document (1, 2).
  Source=System.Xml
  StackTrace:
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
       at Sandbox.Form1.button56_Click(Object sender, EventArgs e) in c:\HoldingTank\Sandbox\Form1.cs:line 2061
    . . .
       StackTrace:
            at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read3_ArrayOfSiteQuery()
       InnerException: 

这怎么可能? “testData”字符串的内容是:

<SiteQuery><Id>00006000002</Id><UPCPackSize>1</UPCPackSize><UPC_Code>00006000002</UPC_Code><crvId></crvId><dept>8</dept><description>ZZ</description><openQty>0.0</openQty><packSize>1</packSize><subDept>80</subDept><unitCost>1.25</unitCost><unitList>5.0</unitList><vendorId>CONFLICT</vendorId><vendorItem>123456</vendorItem></SiteQuery>
. . . // a ton of other StieQuery records
<SiteQuery><Id>5705654</Id><UPCPackSize>1</UPCPackSize><UPC_Code>5705654</UPC_Code><crvId></crvId><dept>2</dept><description>what do you want</description><openQty>0.0</openQty><packSize>1</packSize><subDept>0</subDept><unitCost>0.55</unitCost><unitList>1.62</unitList><vendorId></vendorId><vendorItem></vendorItem></SiteQuery>

怎么会有“ XML文档中的错误(1,2)”

第1行,第2列是“S”;什么是“S”的问题?什么都没有,所以它有什么期望,因为它也不喜欢“A”(来自<ArrayOfSiteQuery)?

更新7

我在前面:

<?xml version="1.0" encoding="UTF-8"?>

...到文件,我得到相同的错误,但现在它是1,40(仍然是第一个“<SiteQuery>”中的“S”)。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

           XmlSerializer serializer = new XmlSerializer(typeof(List<SiteMapping>)); 
            XmlReader reader = XmlReader.Create(new StringReader(xmlData));
            List<SiteMapping> siteMappings;
            siteMappings = (List<SiteMapping>)serializer.Deserialize(reader);

如果有效,请告诉我