0

So i have this code sample... what it should do is... simple read a value from an xml file...

    XDocument xdoc = XDocument.Load(FileLoc);
    MessageBox.Show(xdoc.Descendants("energyPieces").First().Value);

But in the second line i ALWAYS get System.InvalidOperationException... WHILE the xml file IS valid: i checked it with a game(it's actually a game's save file) and checked it with multiple online checkers... Here is the XML file:

<?xml version="1.0" encoding="utf-8"?>
<Save xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DumaLegend">
  <saveInfo>
    <energyPieces>0</energyPieces>
    <fullEnergyCells>4</fullEnergyCells>
    <fullHearts>4</fullHearts>
    <globalSwitches xmlns:d3p1="a">
      <d3p1:switchList xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
    </globalSwitches>
    <gold>0</gold>
    <hasBigFireball>false</hasBigFireball>
    <hasCombo>false</hasCombo>
    <hasCrossbow>false</hasCrossbow>
    <hasDash>false</hasDash>
    <hasDashUpgrade>false</hasDashUpgrade>
    <hasDoubleJump>false</hasDoubleJump>
    <hasFireball>false</hasFireball>
    <hasHookshot>false</hasHookshot>
    <hasInvisPot>false</hasInvisPot>
    <hasSecondCombo>false</hasSecondCombo>
    <hasShieldUpgrade>false</hasShieldUpgrade>
    <hasSmallFireball>false</hasSmallFireball>
    <heartPieces>0</heartPieces>
    <heroPosOnMap>0</heroPosOnMap>
    <heroTokens>0</heroTokens>
    <itemSlot1 xmlns:d3p1="http://schemas.datacontract.org/2004/07/DumaLegend.Objects.Consumables" i:nil="true" />
    <itemSlot2 xmlns:d3p1="http://schemas.datacontract.org/2004/07/DumaLegend.Objects.Consumables" i:nil="true" />
    <lives>3</lives>
    <worldsUnlocked>0</worldsUnlocked>
    <worldsUnlockedOnMap>0</worldsUnlockedOnMap>
  </saveInfo>
  <saveSlot>0</saveSlot>
</Save>

Note: Sorry for copy-pasting it as html code I actually failed to put it in a code sample!

Edit: expected output: 0

1 Answer 1

1

You need a fully qualified name (i.e. including the namespace). Thus

XDocument xdoc = XDocument.Load(FileLoc);
MessageBox.Show(xdoc.Descendants(XName.Get("energyPieces", "http://schemas.datacontract.org/2004/07/DumaLegend")).First().Value);

or, if you prefer

XDocument xdoc = XDocument.Load(FileLoc);
MessageBox.Show(xdoc.Descendants("{http://schemas.datacontract.org/2004/07/DumaLegend}energyPieces").First().Value);
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.