tags:

views:

124

answers:

4

I have the following XML file with "page" nodes which I want to read into "PageItem" objects in my application. Most of the XML nodes I save as string/int/DataTime property which works fine.

But what is the best way to store the XML child nodes of the "content" node in a property of a PageItem object so that I can parse it later within the application? Should I store it as a string and later read in the string as XML or is there a more efficient way to store the XML child nodes as a property?

<?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>
    ...

I read this XML file into PageItem objects:

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

with this 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
    };
+1  A: 

Use this property:-

 public XElement Content { get; set; }  //PSEUDO-CODE

and assign like this:-

 Content = pageItem.Element("content")
AnthonyWJones
thanks XElement was what I was looking for.
Edward Tanguay
+1  A: 

It depends on what you mean by efficient: which side of the space/time trade-off do you want to land? If you are most interested in conserving memory, storing the content as a string and parsing as needed is probably most efficient. But if you are going to be accessing the content frequently and want accesses to be fast, you might want to store it as a list of XElement objects (assuming you are using .Net 3.5, which your sample indicates you are).

Samuel Jack
+4  A: 

Have you tried XmlSerializer?

            public string Serialize(PageItem obj)
         {

          XmlSerializer formatter = new XmlSerializer(typeof(PageItem));

          MemoryStream ms = new MemoryStream();

          formatter.Serialize(ms, obj);

          ms.Position = 0;
          StreamReader sr = new StreamReader(ms);

          return sr.ReadToEnd();
         }

         public PageItem  Deserialize(string serializedObject)
         {

          XmlSerializer formatter = new XmlSerializer(typeof(PageItem));

          MemoryStream ms = serializedObject.ToMemoryStreamFromUTF8();

          return ((PageItem)formatter.Deserialize(ms));
         }
Jan Remunda
+1 for serialization
abatishchev
A: 

Assuming that you application just needs to treat the content as a blob of data (that gets passed to another tier for rendering), just encode it as a base64 string. There are base64 encoding libraries for pretty much any language. For instance, if this is a web app you can pass the string to JavasSript and let it decode and render the content or do it on the server side.

Rodrick Chapman