This is a migrated thread and some comments may be shown as answers.

Loading from XML as shown in documentation

3 Answers 76 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joseph Gershgorin
Top achievements
Rank 1
Joseph Gershgorin asked on 07 Jun 2010, 09:55 AM
I'm trying to follow the example on how to load XML data into a user defined object as described here:

http://www.telerik.com/help/silverlight/gridview-loading-data-from-xml.html

My xml data is structured like this:
<?xml version="1.0"?> 
<log> 
<entry id="0" timestamp="2010-06-07T01:34:36" username="" source="db">Log Created</entry> 
<entry id="1" timestamp="2010-06-07T01:36:36" username="" source="db">Update Check</entry> 
</log> 

Using the example I can access all the attributes fine, but how do I retrieve the value of the entry tag itself?







3 Answers, 1 is accepted

Sort by
0
Joseph Gershgorin
Top achievements
Rank 1
answered on 07 Jun 2010, 10:02 AM
I found the following alternative to work (but I would still like to know how to get the above version to work):

XElement doc = XElement.Parse(filteredXmlString); 
 
var messagesList = (doc.Elements().Select(el => this.GetEntry(el))).ToList(); 
 
Messages = new ObservableCollection<entry>(messagesList); 
 
private entry GetEntry(XElement el) 
    entry en = new entry(); 
    en.message = el.Value; 
    en.id = el.Attribute("id").Value; 
    en.source = el.Attribute("source").Value; 
    en.timestamp = el.Attribute("timestamp").Value; 
    en.username = el.Attribute("username").Value; 
    return en; 

0
Accepted
Veselin Vasilev
Telerik team
answered on 09 Jun 2010, 04:16 PM
Hi Joseph Gershgorin,

You need to use the [XmlText] attribute for your property.

Assume that the example was like this:

<?xml version="1.0" encoding="utf-8" ?>
<Items>
    <XmlNodeItem Team="Barcelona" Country="Spain">test1</XmlNodeItem>
    <XmlNodeItem Team="Juventus" Country="Italy">test2</XmlNodeItem>
    <XmlNodeItem Team="Inter" Country="Italy">test3</XmlNodeItem>
    <XmlNodeItem Team="Ac Milan" Country="Italy">test4</XmlNodeItem>
</Items>

Then your XmlNodeItem class should look like this:

public class XmlNodeItem
{
    [XmlAttribute( AttributeName = "Team" )]
    public string Team
    {
        get;
        set;
    }
    [XmlAttribute( AttributeName = "Country" )]
    public string Country
    {
        get;
        set;
    }
    [XmlText]
    public string nodeElement { get; set; }
}

Hope this helps.

Sincerely yours,
Veskoni
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Joseph Gershgorin
Top achievements
Rank 1
answered on 10 Jun 2010, 01:43 AM
Thank you!
Tags
GridView
Asked by
Joseph Gershgorin
Top achievements
Rank 1
Answers by
Joseph Gershgorin
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Share this question
or