-2

I have a problem again. My problem is that I don't know how to store hyperlink in XML file and how to retrive it from there to WPF ListBox. In my application I write XML file in the following way:

<?xml version="1.0" encoding="utf-8" ?>
<Books xmlns="">
  <Category name="Computer Programming">
    <Book>
      <Author>H. Schildt</Author>
      <Title>C# 4.0 The Complete Reference</Title>
      <!--This is a hyperlink description-->
      <Link>https://www.mcgraw-hill.co.uk/html/007174116X.html</Link>
    </Book>
  </Category>
</Books>

In XAML in Window Resources section I write in the following way:

<Window.Resources>
  . . . . . . . . . .
  <DataTemplate x:Key="detailDataTemplate">
     <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding XPath=Title}"/>
        <TextBlock Text="{Binding XPath=Author}"/>
        <TextBlock Text="{Binding XPath=Link}"/>
     </StackPanel>
  </DataTemplate>
  . . . . . . . . . . .
</Window.Resources>

And finally in ListBox markup I write:

<ListBox Name="lbxBooks" Grid.Row="1" Grid.Column="0"
. . . . . . . . . .
ItemTemplate="{StaticResource detailDataTemplate}" 
            IsSynchronizedWithCurrentItem="True"/>

But after the start of application, the hyperlink is displayed on the screen as simple string that is not enabled and not allowed to mouse click for getting of internet resource with it. So it doesn't work properly as reference to internet resource. It displayed as simple text string. How can I correct this error and force the link to work properly? What do I have to correct in XML file and in XAML for it? I'll appreciate your help very high.

user1688773
  • 157
  • 1
  • 3
  • 11

1 Answers1

0

@ Hyperlink is a string
@ Hyperlink in WPF exmaple look here
@ generic XML tool :

  public class SerializationHelper
    {
        public static void SerializeToXML<T>(T t, String inFilename) where T : class
        {
            StreamWriter textWriter = null;

            try
            {
                var serializer = new XmlSerializer(t.GetType());
                textWriter = new StreamWriter(inFilename);
                serializer.Serialize(textWriter, t);

            }
            finally
            {
                if (textWriter != null) textWriter.Close();
            }
        }

        public static T DeserializeFromXML<T>(String inFilename) where T : class
        {
            TextReader textReader = null;
            T retVal = default(T);
            try
            {
                var deserializer = new XmlSerializer(typeof(T));
                textReader = new StreamReader(inFilename);
                retVal = (T)deserializer.Deserialize(textReader);
                return retVal;

            }
            finally
            {
                if (textReader != null) textReader.Close();
            }
        }
    }



        public class Book
        {
            public string Author { get; set; }
            public string Title { get; set; }
            public string Link { get; set; }
            public string Catgegory { get; set; }
        }

        public class Data
        {
            public List<Book> Books { get; set; }

            public Data()
            {
                Books = new List<Book>();
            }
        }

               // ....
               var filePath = "fileName.xml";
               var bookList = new List<Book>();
                bookList.Add(new Book
                {
                    Author = "H. Schildt",
                    Link = @"https://www.mcgraw-hill.co.uk/html/007174116X.html",
                    Catgegory= "Computer Programming",
                    Title = "C# 4.0 The Complete Reference",
                });

                // save
                SerializeToXML(new Data { Books = bookList }, filePath);
                // load
                var data = DeserializeFromXML<Data>(filePath);
Community
  • 1
  • 1
Zakos
  • 1,492
  • 2
  • 22
  • 41
  • I bag your pardon but I'm newbie in XML. Give me please the ready-made example so I can easily examine it. Please. – user1688773 May 25 '14 at 10:34