2010-08-06 75 views
1
XElement xml = new XElement("MyMenu", 
        from c in db.Security_Module_Menus 
        //where (c.ParentID == 0) 
        orderby c.Menu_ID 
        select new XElement("Item", 
           new XAttribute("Text", c.Menu_Name), new XAttribute("NavigateUrl", c.Target_URL) 


           ) 
        ); 

從我上面的語法c.Menu_Name,c.Target_URL值nullable.Show我的錯誤設置如何XAttribute NULL值

值不能爲空。 參數名:用來解決這個error.But我如何使用我的上述syntax.Help我值我知道SetElementValue()使用it.Show我語法

回答

2

大概要避免創建屬性如果c.Menu_Name和c.Target_URL爲空?

new XElement("MyMenu", 
    from c in db.Security_Module_Menus 
    orderby c.Menu_ID 
    select new XElement("Item", 
    c.Menu_Name == null ? null : new XAttribute("Text", c.Menu_Name), 
    c.Target_URL == null ? null : new XAttribute("NavigateUrl", c.Target_URL)) 
) 
+0

感謝您的幫助。我有Menu_ID和PARENT_ID columns.want創建 ... ..... ...對父ID的基礎:如果是這樣,你可以按如下做到這一點。 http://stackoverflow.com/questions/3412530/how-to-create-dynamic-aspxmenu-from-database。在這裏你可以看到我的talbe.Plz幫我解決這個問題。再次感謝您的幫助。 – shamim 2010-08-06 03:39:07