1

XML

<?xml version="1.0" encoding="utf-8"?>
<DeviceIdMappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Devices>
    <DeviceEntry>
      <Key>‭‭907579560</Key>
      <Value>SEMROUTER</Value>
    </DeviceEntry>
  </Devices>
</DeviceIdMappings>

CODE

string filename = @"C:\Users\Desktop\DeviceMapping.xml";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.PreserveWhitespace = true;
            xmlDoc.Load(filename);
            var xml = XElement.Load(filename);
            Dictionary<uint, string> MasterDeviceMappings = (from x in xml.Descendants("DeviceEntry")
                                    select new
                                    {
                                        Key = x.Element("Key").Value,
                                        Value = x.Element("Value").Value,
                                    }
                        ).ToDictionary(q => uint.Parse(q.Key), q => q.Value);

Exexption System.FormatException: 'Input string was not in a correct format.' Exception Image

No idea whats wrong here. uint.Parse("907579560") works.

Community
  • 1
  • 1
sPS
  • 71
  • 8
  • What's wrong is the input isn't valid. – 15ee8f99-57ff-4f92-890c-b56153 Nov 06 '19 at 16:38
  • 2
    Since you've got a screenshot of the debugger, why not see what `q.Key` contains...? – canton7 Nov 06 '19 at 16:38
  • 3
    When I paste your XML into visual studio, I see two non-numeric characters prefixed to the value for `Key` `??907579560`. Some weird Unicode thing I expect. Just clean the XML file, and possibly also fix whatever generates it. – 15ee8f99-57ff-4f92-890c-b56153 Nov 06 '19 at 16:41
  • q.Key contains ‭‭907579560 – sPS Nov 06 '19 at 16:41
  • @EdPlunkett wow, how did you see that i have opened the same file in visual studio!! – sPS Nov 06 '19 at 16:43
  • I copied from the web page and pasted it through `sed` to double the double quotes, so I could paste it into an @"" string in my C# file. It came out like that, with the question marks. Pasting it in directly from this page leaves those characters non-visible. So it was either my command line paste-to-stdout utility, sed, or my command line copy-to-clipboard-from-stdin utility. Cue twilight zone music. – 15ee8f99-57ff-4f92-890c-b56153 Nov 06 '19 at 16:45
  • 3
    Specifically, there are 2 null bytes after `` and before `907579560` – canton7 Nov 06 '19 at 16:46
  • any tools i can see those chars or clean this up !!! VS does not showing those then !! – sPS Nov 06 '19 at 16:47
  • 1
    I pasted into HxD (Notepad++ also works if you select ANSI), but the easiest thing to do is going to be to delete that `...` line and re-type it by hand. – canton7 Nov 06 '19 at 16:48
  • 2
    You can't mix XmlElement with XElement. Use XDocument xmlDoc = new XDocument(); – jdweng Nov 06 '19 at 16:48
  • @EdPlunkett and canton7 Yes some nonsense cahr are there i could change the encoding in notepad++ and see those. Thanks guys really appreciate it. – sPS Nov 06 '19 at 17:14

1 Answers1

0

You have special non printable characters in the string. You can remove them by

uint.Parse(Regex.Replace(q.Key, @"\p{C}+", string.Empty))
Pietro
  • 413
  • 2
  • 11