-1

I have an XML File that looks like this :

<ROOT><DOC> ... </DOC><DOC> ... </DOC><DOC> ... </DOC></ROOT>

I want to put all the DOC in an array. How do I do that in C# (XmlSerializer) ?

3

3 Answers 3

0

In essence, you need a string that contains your XML, a StringReader to read the string, an XMLReader to read the feed from the StringReader and an XDocument to store the feed from the XMLReader. This can be done in a single line of code, like this:

XDocument xDoc = XDocument.Load (XmlReader.Create (new StringReader (xmlString)));

The xmlString is the path (and name) of the file you're reading. You should use a List to store the data you'll get (unless it's a set number, then you can just use a string[]).

List<string> docList = new List<string>();

Then it's a matter of using a foreach loop to go through the XML elements and adding them to your list:

foreach (var element in xDoc.Descendants("ROOT"))
{
    string doc = element.Element ("DOC").Value;
    docList.Add (doc);
}

to make it an array, use:

docList.ToArray();

I hope this helps! Good luck.

1
  • Okay. What if you try this: foreach (var element in xDoc.Element ("DOC").Value) { docList.Add (xDoc.Element ("DOC").Value); } Nov 21, 2014 at 14:34
0

Maybe it depends on the framework version. I have .net v4 and would use the following class with XmlSerializer.

Thanks to @Reniuz for the hint of the error. Here is a full working example:

public class Document
{
    [XmlAttribute]
    public string Value { get; set; }
}

[XmlRoot("ROOT")]
public class Root
{
    [XmlElement("DOC")]
    public List<Document> Documents { get; set; }
}

Using this code to load:

string data = "<ROOT><DOC Value=\"adhfjasdhf\"></DOC><DOC Value=\"asldfhalsdh\"></DOC></ROOT>";
XmlSerializer serializer = new XmlSerializer(typeof(Root));
using (StringReader sr = new StringReader(data))
{
    Root root = serializer.Deserialize(sr) as Root;
}

Keep attantion that the tags are case sensitive.

3
  • XmlElement can be applied for fields and properties. You'll get error. For Root class use XmlRoot attribute
    – Renatas M.
    Nov 21, 2014 at 14:13
  • But XmlRoot doesn't help me because my array is empty. Nov 21, 2014 at 14:24
  • At the end use Document[] documents = root.Documents.ToArray(); to get the array.
    – elite
    Nov 21, 2014 at 14:43
0

This is the right answer, based on Magicbjorn answer :

First of all, i'm getting my string from a StreamReader.

    using(StreamReader read = new StreamReader("FilePath.xml"))
    {
        XDocument xDoc = XDocument.Load(XmlReader.Create(read));
        List<string> docList = new List<string>();

        var root = xDoc.Element("ROOT");
        foreach (var element in root.Elements("DOC"))
        {
            string s = element.Value;
            docList.Add(s);
        }
    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.