0

I have some trouble loading an XML File and turn it into string (like <XBoxIP></XBoxIP> and I want whats in between them); this is my xml file:

<?xml version="1-0" encoding="UTF8"?>
<Config>
    <XBoxIP></XBoxIP>
    <XBoxPort></XBoxPort>
    <XBoxUser></XBoxUser>
    <XBoxPassword></XBoxPassword>
    <XBoxSongPath></XBoxSongPath>
    <LocalSongPath></LocalSongPath>
</Config>

Or is my XML File maybe incorrect? Thanks for the help, i am new to C# and XML.

XmlDocument doc = new XmlDocument();
doc.Load(path + "/Config.xml");
string xmlcontents = doc.InnerXml;

is the code im currently using, and i don't really know what to do after that.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Please show the problematic code also – Rufus L May 04 '18 at 20:21
  • @RufusL `XmlDocument doc = new XmlDocument(); doc.Load(path + "/Config.xml"); string xmlcontents = doc.InnerXml;` and i don't know what to do after that :/ – ChrisderWahre May 04 '18 at 20:23
  • Is your object serializable? – random May 04 '18 at 20:25
  • @random No, i don't think so. – ChrisderWahre May 04 '18 at 20:26
  • Ah, ok. It sounds more like you're looking for a tutorial on how to read an xml file in c#. – Rufus L May 04 '18 at 20:27
  • @RufusL Editited. – ChrisderWahre May 04 '18 at 20:31
  • Yeah im probably looking for that! @RufusL but i don't find examples that fit my xml file. – ChrisderWahre May 04 '18 at 20:37
  • The code you posted works for me. Only thing you need to change is version to `1.0` and encoding to `UTF-8` – gerb0n May 04 '18 at 20:41
  • Maybe you want [Parsing XML using XDocument](https://stackoverflow.com/questions/7798852/parsing-xml-using-xdocument). You should prefer using `XDocument` to the older `XmlDocument`, see [XDocument or XmlDocument](https://stackoverflow.com/q/1542073/3744182). – dbc May 04 '18 at 20:41
  • There is no values in the xml so you should be getting nothing back. – jdweng May 04 '18 at 20:58
  • 1
    On closer inspection, your XML is not valid or well-formed. Upload it to https://www.xmlvalidation.com/ and you will get an error *XML version "1-0" is not supported, only XML 1.0 is supported.* `"1.0"` and `"1.1"` are both [recognized XML versions](https://en.wikipedia.org/wiki/XML#Versions) but `"1-0"` is not. See also https://dotnetfiddle.net/VK1aY0 that shows the same error with .Net. – dbc May 04 '18 at 21:28

1 Answers1

0

I make slight change to your xml and i hope it will work for you.

<?xml version="1.0" encoding="UTF-8"?> //make changes here
<Config>
    <XBoxIP></XBoxIP>
    <XBoxPort></XBoxPort>
    <XBoxUser></XBoxUser>
    <XBoxPassword></XBoxPassword>
    <XBoxSongPath></XBoxSongPath>
    <LocalSongPath></LocalSongPath>
</Config>

you can also used below property. The OuterXml property returns a string version of the xml.

xmlDoc.OuterXml;
Azeem Hafeez
  • 250
  • 4
  • 14