Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey mates,

normally I am using a MS SQL Server to store/load information but now I am playing with UWP on my Lumia and it is great. But now I want to load content from a local file and display it in a Textblock.

I have not worked for years with XML, so here's the file:

XML
<Stations>
  <Station ID="1">
  <Name>ABC</Name>
  <Text>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</Text>
  <Picture>Assets\Pirate.ico</Picture>
  </Station>
  <Station ID="2">
    <Name>DEF</Name>
    <Text>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</Text>
    <Picture>Assets\Pirate.ico</Picture>
  </Station>
</Stations>


My source, does not work:

C#
public void fillContent(int ID)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(@"data\stations.xml");
            MemoryStream stream = new MemoryStream(byteArray);
            doc.Load(stream);
            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                string text = node.InnerText;
                TBText.Text = text;
            }
        }


But I want to load specific data, e.g. something like "WHERE ID= 'ID'", like in SQL so far.

Regards,
Vince

What I have tried:

Reading, searching via google, reading on codeproject.
Posted
Updated 30-Mar-16 23:08pm

1 solution

Your code is trying to load the literal string data\stations.xml as an XML document. It's clearly not XML.

I suspect you wanted to load an XML file from disk; in which case, you just need to pass the path of the file to the Load method:
C#
doc.Load(@"data\stations.xml");

If you're going to be running queries against the XML document, it would probably be easier to use LINQ to XML:
LINQ to XML[^]
C#
XDocument doc = XDocument.Load(@"data\stations.xml");
XElement station = doc.Descendants("Station").FirstOrDefault(s => (int?)s.Attribute("ID") == 1);
 
Share this answer
 
Comments
Haechtsuppe 31-Mar-16 5:09am    
Wow, thank you I have not worked with Linq since years. It works great, I added a function to trim the string so I won't get the tags:

<pre lang="c#"> public void fillContent(int number)
{
XDocument doc = XDocument.Load(@"data\stations.xml");
XElement station = doc.Descendants("Station").FirstOrDefault(s => (int?)s.Attribute("ID") == number).Element("Text");
TBText.Text = removeTag(station);
}
private string removeTag(XElement element)
{
string innerXml = element.ToString().Trim().Replace(string.Format("<{0}>", element.Name), "");
innerXml = innerXml.Trim().Replace(string.Format("", element.Name), "");
return innerXml.Trim();
}</pre>

If I plan in future projects to deploy the application with XML files, do you have a hint how to handle it?
Richard Deeming 31-Mar-16 7:28am    
You don't need the function; just take the element's Value property:
TBText.Text = station.Value;

Or cast the element to a string:
TBText.Text = (string)station;
Haechtsuppe 31-Mar-16 7:32am    
Oh, that works really nice thank you :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900