0

I have an XML File:

<?xml version="1.0" encoding="utf-8"?>
<Options>
  <Gestion>
    <Prêts>
      <NbrRenouvlement>5</NbrRenouvlement>
      <DureeEmprunt>10</DureeEmprunt>
      <NbrEmprunt>3</NbrEmprunt>
    </Prêts>
    <Réservations>
      <NbrReservation>3</NbrReservation>
      <DureeReservation>7</DureeReservation>
    </Réservations>
  </Gestion>
  <Config>
    <RememberMe>False</RememberMe>
    <ConnexionAutomatique>False</ConnexionAutomatique>
    <ShowToolTip>True</ShowToolTip>
  </Config>
</Options>

I want to select the value of NbrRenouvlement which is 5, and this is the code I wrote:

String nbrReservation = String.Empty;
var makeInfo = from reservations in doc.Descendants("Réservations") 
           select reservations.Element("NbrReservation").Value;

but this code is returns a Generic List, inspite of the value I want to select is just one number so I need to work with a foreach loop:

foreach (string s in makeInfo.Distinct()) 
               nbrReservation.Text += s;

This methods works fine but it's too long for select one value, is there any other method that allows me to select the value of NbrReservation from that XML file?

And how can I change this value ?

3
  • You want to get the value of NbrRenouvlement but your code tries to get the value of Réservations/NbrReservation. Can you clarify what you really want to do? Commented Nov 23, 2012 at 23:26
  • I want to get the value as a string not as an IEnumerable<String> Commented Nov 23, 2012 at 23:27
  • Su Sha, What you want to get(NbrRenouvlement) and your code is doing contradicts(you are selecting Réservations/NbrReservation). Commented Nov 23, 2012 at 23:31

3 Answers 3

3

I want to select the value of NbrRenouvlement

You can use XPath (System.Xml.XPath).

var five = xDoc.XPathSelectElement("//NbrRenouvlement").Value;

And this is the code similar to the one in your question

var xDoc = XDocument.Parse(xml);
var makeInfo = xDoc.XPathSelectElement("//Réservations/NbrReservation");
if (makeInfo != null)
{
    var val = makeInfo.Value;
    //makeInfo.Value = newValue;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I have a question about this: XDocument.Parse(xml); am I should change the parametre xml by the path of my xml file or by XDocument.Load(filePath)
Parse is used to load the xml from a string variable. If you load the xml from a file, use Load
I tried this code but it didn't worked it returns an empty string
@SuSha What did you try? Since i tested it before posting and it works. Please post your code.
Sorry it's my false I missed a casting
1

This will give you a list of string (IEnumerable<string>):

var makeInfo = from reservations in doc.Descendants("Réservations") 
       select reservations.Element("NbrReservation").Value;

If you rephrase it as:

var makeInfo = (from reservations in doc.Descendants("Réservations") 
       select reservations.Element("NbrReservation")).First().Value;

Then you can do this:

nbrReservation.Text += s;

This is assuming that NbrReservation node is guaranteed to be in the XML.

Comments

1

Reading and saving value:

XDocument xdoc = XDocument.Load(path_to_xml);
var nbrReservation = xdoc.Descendants("Réservations")
                         .Single() // if you have single reservations node
                         .Element("NbrReservation");
int value = (int)nbrReservation;
nbrReservation.SetValue(42);
xdoc.Save(path_to_xml);

4 Comments

The value of NbrRenouvlement can be changed
@SuSha then why you are asking to get value of NbrRenouvlement which is 5? (see my quote at the top of answer)
because in the example I gave the the value of NbrRenouvlement is 5 but this valu can be changed
@SuSha so you want to get this node by name, not by value?

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.