0

I want to create a program for my church who gets songs from a save file. But I don't know how to put that data into an multidimensional array. Below are the things I want to be put into an array. Is that possible? (I'm new to C#)

<items>
  <CMediaBase xsi:type="CSong">
     <DisplayTitle>Eigen bundel 10 - In Jesus name</DisplayTitle>
     <IsDirty>false</IsDirty>
     <SwapLanguages>false</SwapLanguages>
     <SongLanguage>Both</SongLanguage>
     <MediaType>Song</MediaType>
     <Number>10</Number>
     <SelectedVersion>MyVersion</SelectedVersion>
     <SongBookName>Eigen bundel</SongBookName>
     <ThemeName>OPS</ThemeName>
     <Title>In Jesus name</Title>
  </CMediaBase>
</items>
1
  • Why do you want to use a multi-dimensional array? They are rarely useful and I can't see how they would be in this circumstance. Commented Feb 10, 2019 at 11:01

1 Answer 1

3

I think you are not using the appropriate data structures to solve the problem.

A two-dimensional array will not solve the problem here which boils down to parsing your songs XML file and transforming that data representation to something that you can use in C#.

I propose to represent your songs with a C# class, something like this:

[XmlRoot("SongList")]
public class SongList
{
    [XmlElement("Song")]
    public List<Song> Songs { get; set; }
}

public class Song
{
    [XmlElement("DisplayTitle")]
    public string DisplayTitle { get; set; }

    [XmlElement("IsDirty")]
    public bool IsDirty { get; set; }

    [XmlElement("SwapLanguage")]
    public bool SwapLanguage { get; set; }

    [XmlElement("SongLanguage")]
    public string SongLanguage { get; set; }

    [XmlElement("MediaType")]
    public string MediaType { get; set; }

    [XmlElement("Number")]
    public int Number { get; set; }

    [XmlElement("SongBookName")]
    public string SongBookName { get; set; }

    [XmlElement("ThemeName")]
    public string ThemeName { get; set; }

    [XmlElement("Title")]
    public string Title { get; set; }
}

Please note the use of the [XmlElement] and [XmlRoot] attributes, which allow us to specify to the XML serializer how to parse your XML representation to a C# representation of that XML.

Now using an XML string like the following :

 string xml = @"<SongList>
 <Song>
   <DisplayTitle> Eigen bundel 10 - In Jesus name</DisplayTitle>
   <IsDirty > false </IsDirty>
   <SwapLanguages >false </SwapLanguages>
   <SongLanguage > Both </SongLanguage >
   <MediaType >Song</MediaType >
   <Number>10</Number >
   <SelectedVersion >MyVersion</SelectedVersion >
   <SongBookName > Eigen bundel</SongBookName >
   <ThemeName >OPS</ThemeName >
   <Title >In Jesus name</Title>
 </Song>
 <Song>
   <DisplayTitle > Song 2</DisplayTitle >
   <IsDirty > true </IsDirty >
   <SwapLanguages > false </SwapLanguages >
   <SongLanguage > Both </SongLanguage >
   <MediaType > Song </MediaType >
   <Number > 10 </Number >
   <SelectedVersion > MyVersion </SelectedVersion >
   <SongBookName > Eigen bundel </SongBookName >
   <ThemeName > OPS </ThemeName >
   <Title > In Jesus name</Title >
   </Song>
 </SongList>";

You can then use the C# serializer to deserialize this XML representation to the object representation as follows:

var serializer = new XmlSerializer(typeof(SongList));

using (var reader = new StringReader(xml))
{
   var albums = serializer.Deserialize(reader) as SongList;
}

If you want to read a text file you should load the file into a FileStream and deserialize it like this:

using (var fs= new FileStream("<YOUR XML FILE PATH>", FileMode.Open)) 
{
  var songList = serializer.Deserialize(fs) as SongList;
}

Note that you should be importing the following namespaces to be able to use the XML serializer and the FileStream class :

using System.IO;
using System.Xml;
using System.Xml.Serialization;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.