2013-06-28 50 views
2

這裏是XML:解析XML轉換成目標

<?xml version="1.0" encoding="utf-8" ?> 
    <object> 
    <body>tests</body> 
    <send_results type="null" /> 
    <note_class>none</note_class> 
    <users type="list" /> 
    <title>test</title> 
    <time_sent type="null" /> 
    <image type="null" /> 
    <to_customers type="boolean">False</to_customers> 
    <time_created>2013-06-26T16:40:50</time_created> 
    <num_sends type="integer">0</num_sends> 
    <time_scheduled type="null" /> 
    <dealership>/api/v1/dealerships/10/</dealership> 
    <id type="integer">22</id> 
    <send_rate>none</send_rate> 
    <send_method>email</send_method> 
    <reply_to_email>[email protected]</reply_to_email> 
    <response_link type="null" /> 
    <to_users type="boolean">True</to_users> 
    <resource_uri>/api/v1/notifications/22/</resource_uri> 
    </object> 

我試圖將XML解析成一個對象...

 var query = from m in parsedXml.Decendants("object") 
        select new Notifications 
        { 
         Body = (string)m.Element("body"), 
         SendResults = (string)m.Element("send_results"), 
         NoteClass = (string)m.Element("note_class"), 
         Users = m.Element("users").Elements().Select(e => (string)e.Element("value")).ToList(), 
         Title = (string)m.Element("title"), 
         TimeSent = (string)m.Element("time_sent"), 
         Image = (string)m.Element("image"), 
         ToCustomers = (string)m.Element("to_customers"), 
         TimeCreated = (string)m.Element("time_created"), 
         NumSends = (string)m.Element("num_sends"), 
         TimeScheduled = (string)m.Element("time_scheduled"), 
         Dealership = (string)m.Element("dealership"), 
         Id = (string)m.Element("id"), 
         SendRate = (string)m.Element("send_rate"), 
         SendMethod = (string)m.Element("send_method"), 
         ReplyToEmail = (string)m.Element("reply_to_email"), 
         ResponseLink = (string)m.Element("response_link"), 
         ToUsers = (string)m.Element("to_users"), 
         ResourceUri = (string)m.Element("resource_uri"), 
        }; 

,我不斷收到「無」或「空「在變量」查詢「中。

我無法弄清楚這一點 - 我嘗試了很多不同的東西。感謝您在這些問題上的幫助。

+6

請包括如何創建'parsedXml'的代碼 –

+3

您可以編譯該代碼嗎?因爲它是'後裔'不''Decendants' – I4V

+0

http://broadcast.oreilly.com/2010/10/understanding-c-simple-linq-to.html像pswg張貼,除非我們看到它是如何創建。但你也可以點擊這個鏈接查看 – KeyboardFriendly

回答

2

我複製了你的代碼,並直接將它粘貼到控制檯應用程序中。它完全按預期工作。

enter image description here

還有什麼,你可以告訴我們,可能對結果產生影響?

這裏是我使用的完整樣本,如果你正在做的事情不同的...

string xml = 
@"<?xml version='1.0' encoding='utf-8' ?> 
    <object> 
    <body>tests</body> 
    <send_results type='null' /> 
    <note_class>none</note_class> 
    <users type='list' /> 
    <title>test</title> 
    <time_sent type='null' /> 
    <image type='null' /> 
    <to_customers type='boolean'>False</to_customers> 
    <time_created>2013-06-26T16:40:50</time_created> 
    <num_sends type='integer'>0</num_sends> 
    <time_scheduled type='null' /> 
    <dealership>/api/v1/dealerships/10/</dealership> 
    <id type='integer'>22</id> 
    <send_rate>none</send_rate> 
    <send_method>email</send_method> 
    <reply_to_email>[email protected]</reply_to_email> 
    <response_link type='null' /> 
    <to_users type='boolean'>True</to_users> 
    <resource_uri>/api/v1/notifications/22/</resource_uri> 
    </object>"; 
var parsedXml = XDocument.Parse(xml); 
var query = from m in parsedXml.Descendants("object") 
      select new Notifications 
      { 
       Body = (string)m.Element("body"), 
       SendResults = (string)m.Element("send_results"), 
       NoteClass = (string)m.Element("note_class"), 
       Users = m.Element("users").Elements().Select(e => (string)e.Element("value")).ToList(), 
       Title = (string)m.Element("title"), 
       TimeSent = (string)m.Element("time_sent"), 
       Image = (string)m.Element("image"), 
       ToCustomers = (string)m.Element("to_customers"), 
       TimeCreated = (string)m.Element("time_created"), 
       NumSends = (string)m.Element("num_sends"), 
       TimeScheduled = (string)m.Element("time_scheduled"), 
       Dealership = (string)m.Element("dealership"), 
       Id = (string)m.Element("id"), 
       SendRate = (string)m.Element("send_rate"), 
       SendMethod = (string)m.Element("send_method"), 
       ReplyToEmail = (string)m.Element("reply_to_email"), 
       ResponseLink = (string)m.Element("response_link"), 
       ToUsers = (string)m.Element("to_users"), 
       ResourceUri = (string)m.Element("resource_uri"), 
      }; 

Console.WriteLine("Number of items = {0}, {1}",query.Count(), query.FirstOrDefault().Body); 
Console.ReadLine(); 
+2

儘管這是一個很好的觀點,但它並沒有回答這個問題。 –

+0

這點很重要。我懷疑這個問題是由於除了已經介紹的東西之外的事情。我希望如果他/她仍然存在問題,他可以提供更多細節。 –

+0

這應該仍然是一條評論。 –

0

您還沒有表現出被解析你的XML,我們理所當然可以正確完成。可以肯定的是,你應該從文件中加載它或者像這樣解析一個字符串。

//Load Xml file 
var document = XDocument.Load(@"c:\temp\test.xml"); 

//Parse Xml 
var document = XDocument.Parse(xml); 

如果您放置在線路開始var query一個斷點,那麼當它擊中檢查parsedXml以確保它的預期值。

另外你應該真的有這樣的事情,你目前缺少一個根節點。

<?xml version="1.0" encoding="utf-8" ?> 
    <objects> 
    <object> 
     ... 
    <object> 
    </objects> 

最後,如果你已經有一個類,這個反序列化正確,使用LINQ是不同於反序列化。如果你只是選擇一個現有的類,那麼LINQ就是你想要的。確保你使用正確的工具來完成這項工作。