2016-03-18 5 views
0

J'ai un problème, lorsque je tente de lire le fichier XML, qui a des valeurs décimales, comme 24,5478785 - il rend erreur:Impossible de lire les nombres décimaux de XML

There is error in XML document

Quelqu'un peut-il s'il vous plaît conseiller, pourquoi est cela arrive? partie décimale dans le fichier ressemble: <interval>22,555555</interval>

Mon code:

private void OpenFileXml(bool runIt, string file) 
{ 
    //Get data from XML file 
    XmlSerializer ser = new XmlSerializer(typeof(ActionsEntry)); 
    using (FileStream fs = System.IO.File.Open(file, FileMode.Open)) 
    { 
     try 
     { 
      ActionsEntry entry = (ActionsEntry)ser.Deserialize(fs); 
      lvActions.Items.Clear(); 
      foreach (ActionsEntryAction ae in entry.Action) 
      { 
       string point = ae.X.ToString() + "," + ae.Y.ToString(); 
       string interval = (ae.interval).ToString("F6"); 
       ListViewItem lvi = new ListViewItem(new string[] { point, ((ClickType)(ae.Type)).ToString(), interval, ae.Text }); 
       ActionEntry acion = new ActionEntry(ae.X, ae.Y, ae.Text, ae.interval, (ClickType)(ae.Type)); 
       lvi.Tag = acion; 
       lvActions.Items.Add(lvi); 
      } 

      if (runIt) 
      { 
       btnStart.PerformClick(); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Clicer", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 
} 

EDIT

fichier XML: enter image description here

XML:

<?xml version="1.0" encoding="utf-8"?><ActionsEntry xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Action> 
<X>824</X> 
<Y>456</Y> 
<Text /> 
<interval>22,555555</interval> 
<Type>0</Type> 
</Action> 
</ActionsEntry> 
+0

Avez-vous essayé de changer le CultureInfo? –

+0

Ne pas vraiment avoir la moindre idée, qu'est-ce que c'est ... – JustinasT

+2

Jetez un oeil à cette question: http://stackoverflow.com/questions/17437946/xml-deserialization-crashes-on-decimal-parse-due-to -formating Je pense qu'il a la réponse que vous cherchez. –

Répondre

0

Le sérialiseur XML utilise un format standard pour sérialiser/désérialiser le temps flottant/date et il ne se soucie pas vraiment de CultureInfo. Ce problème est dû à la conversion flottante dans les threads cultureInfo.

Cela peut ne pas être la meilleure option, mais vous pouvez essayer de modifier les informations de culture avant la sérialisation.

System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); 
customCulture.NumberFormat.NumberDecimalSeparator = ","; 

System.Threading.Thread.CurrentThread.CurrentCulture = customCulture; 

ou vous pouvez implémenter un sérialiseur personnalisé. voici un bel article

Injecting XML Serialization for formatting decimal properties

Une autre option est de changer ActionsEntry classe et mettre en œuvre une propriété wrapper pour convertir interval et ignorer thread's cultureInfo pour résoudre ce problème.

public class ActionsEntry 
{ 
    public Action Action { get; set; } 
} 

public class Action 
{ 
    public int X { get; set; } 

    public int Y { get; set; } 

    public string Text { get; set; } 

    [XmlIgnore] 
    public float interval { get; set; } 

    private string _intervalString; 
    [XmlElement("interval")] 
    public string IntervalString 
    { 
     get 
     { 
      return _intervalString; 
     } 
     set 
     { 
      _intervalString = value; 
      if (!string.IsNullOrEmpty(value)) 
      { 
       interval = float.Parse(value, CultureInfo.InvariantCulture); 
      } 
     } 
    } 

    public int Type { get; set; } 
} 
+0

Pas de chance, même erreur. – JustinasT

+0

dans quelle ligne il soulève l'exception? Où as-tu ajouté ce code? – esiprogrammer

+0

avez-vous essayé de remplacer l'élément xml par ' 22.555555' remplacer ',' par '.' fonctionne-t-il de cette façon ou pas? – esiprogrammer

0

You have not provided the structure of your Action and ActionsEntry classes, so I went ahead with following assumptions

public class ActionsEntry 
    { 
     public ActionsEntry() { Actions = new List<Action>(); } 
     [XmlElement("Action")] 
     public List<Action> Actions { get; set; } 

    } 

    public class Action 
    { 
     public int X { get; set; } 
     public int Y { get; set; } 

     public string Text { get; set; } 

     [XmlElement("interval")] 
     public string Interval { get; set; } 
     public int Type { get; set; } 

    } 

Ensuite, dans la boucle, j'ai essayé:

ActionsEntry entry = (ActionsEntry)ser.Deserialize(fs); 
foreach (var action in entry.Actions) 
{ 
    string interval = action.Interval.Replace(',', '.'); 
    decimal intvrl = Decimal.Parse(interval); 
    Console.WriteLine(intvrl); 
}