First of all, i'm sorry, i know my question will be very basic and stuff but please i would really appreciate some help, i'm under lots of pressure, so making smart comments and being ironic wouldn't help
I have this code: At least i'm trying. I have this xml file, i need to save all it's attributes in a class so i can just duplicate it whenever i need to.
I don't know how to.
public static void firstFileXml(string sXml1)
{
var root = XElement.Load(@"sXml1");
var controlElementsFirst = root.Descendants("books");
}
The XML file has attributes like: label, text, label_w etc. I need a function or somethign that will allow me to enter like: explore(xmlLocation) and to do the rest. Because i need to do it for a few xml files
I need to build a class that will allow me to read it. Suppose i have this file of xml:
WAS
<books>
<book label='' page='' intro =''/>
<book label='' page='' intro =''/>
<book label='' page='' intro =''/>
</books>
IS
<SHEET>
<books>
<book label='1' page='1' intro='1'/>
<book label='2' page='2' intro='2'/>
<book label='3' page='3' intro='3'/>
</books>
</SHEET>
And so on. I need first to read this xml file then store the book attributes in a class, so i can use it for hundreds of books later
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static class Program
{
static void Main()
{
// get the xml as a string; note that we could also use
// Deserialize(Stream) to process a FileStream, but this
// works fine
string xml = File.ReadAllText(@"C:\Users\books.xml");
var ser = new XmlSerializer(typeof(BookRoot));
var root = (BookRoot)ser.Deserialize(new StringReader(xml));
foreach (var book in root.Books)
{
Console.WriteLine("label: " + book.Label);
Console.WriteLine("page: " + book.Page);
Console.WriteLine("intro: " + book.Intro);
Console.ReadLine();
}
}
}
[XmlRoot("SHEET")]
public class BookRoot
{
private readonly List<Book> books = new List<Book>();
[XmlArray("books"), XmlArrayItem("book")]
public List<Book> Books { get { return books; } }
}
public class Book
{
[XmlAttribute("label")]
public string Label { get; set; }
[XmlAttribute("page")]
public string Page { get; set; }
[XmlAttribute("intro")]
public string Intro { get; set; }
}