我有以下的XML文件「頁面」節點我想在我的應用程序中讀入「PageItem」對象。大部分XML節點我保存爲string/int/DataTime屬性,可以正常工作。在.NET應用程序中傳遞XML的最佳方式是什麼?
但是什麼是將「content」節點的XML子節點存儲在PageItem對象的屬性中以便我可以稍後在應用程序中解析它的最佳方式是什麼?我應該將它作爲一個字符串存儲並稍後以字符串形式讀取爲XML還是存在將XML子節點作爲屬性存儲的更有效方法?
<?xml version="1.0" encoding="utf-8" ?>
<pages>
<page>
<id>1</id>
<whenCreated>2007-01-19T00:00:00</whenCreated>
<itemOwner>edward</itemOwner>
<publishStatus>published</publishStatus>
<correctionOfId>0</correctionOfId>
<idCode>contactData</idCode>
<menu>mainMenu</menu>
<title>Contact Data</title>
<description>This is contact data page.</description>
<accessGroups>loggedInUsers,loggedOutUsers</accessGroups>
<displayOrder>10</displayOrder>
<content>
<form idcode="customers" query="limit 5; category=internal"/>
<form idcode="customersDetail" query="limit 10; category=internal"/>
</content>
</page>
<page>
<id>2</id>
<whenCreated>2007-01-29T00:00:00</whenCreated>
<itemOwner>edward</itemOwner>
...
我看了這個XML文件到PageItem對象:
public class PageItem : Item
{
public string IdCode { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Menu { get; set; }
public string AccessGroups { get; set; }
public int DisplayOrder { get; set; }
public List<XmlNode> Content { get; set; } //PSEUDO-CODE
與此代碼:
var pageItems = from pageItem in xmlDoc.Descendants("page")
orderby (int)pageItem.Element("displayOrder")
select new Models.PageItem
{
Id = (int)pageItem.Element("id"),
WhenCreated = (DateTime)pageItem.Element("whenCreated"),
ItemOwner = pageItem.Element("itemOwner").Value,
PublishStatus = pageItem.Element("publishStatus").Value,
CorrectionOfId = (int)pageItem.Element("correctionOfId"),
IdCode = pageItem.Element("idCode").Value,
Menu = pageItem.Element("menu").Value,
Title = pageItem.Element("title").Value,
Description = pageItem.Element("description").Value,
AccessGroups = pageItem.Element("accessGroups").Value,
DisplayOrder = (int)pageItem.Element("displayOrder"),
Content = pageItem.Element("content").DescendantNodes() //PSEUDO-CODE
};
謝謝XElement就是我一直在尋找的東西。 – 2009-06-04 09:12:33