简体   繁体   English

XML-将值转换为特定格式

[英]XML - Convert Value to Specific Format

I'm loading a XML file into a GRIDVIEW and I managed to do so. 我正在将XML文件加载到GRIDVIEW中,并设法做到了。 But I'm currently stuck in one thing and that is, when I try to load one specific value (47.138). 但是我目前陷入一件事,那就是当我尝试加载一个特定值(47.138)时。 This value is supposed to be loaded in the following format (Minutes:Seconds.Mileseconds). 该值应该以以下格式(Minutes:Seconds.Mileseconds)加载。 Which in this case would be "0:47.138" . 在这种情况下为“ 0:47.138”。

My question is, how do I load this type of data and show it in the correct format? 我的问题是,如何加载此类数据并以正确的格式显示?

XmlNodeList drivers = doc.GetElementsByTagName("Driver");

        foreach (XmlNode driver in drivers)
        {
            dInfo = new BusObjects.DriverInfo();

if (driver.ChildNodes[i].Name.Equals("BestLapTime"))
                    dInfo.FastestLap = Convert.ToDouble(driver.ChildNodes[i].InnerText).ToString("????");                     
            }

This is my DriverInfo.cs 这是我的DriverInfo.cs

    private double _fastestlap;

    public double FastestLap
    {
        get { return _fastestlap; }
        set { _fastestlap = value; }
    }

You can create a TimeSpan object and use that to format the text. 您可以创建一个TimeSpan对象,并使用该对象设置文本格式。

TimeSpan ts = TimeSpan.FromSeconds(47.138);
Console.WriteLine(ts.ToString(@"mm\:ss\.fff"));

In your DriverInfo class, you could have a method to do this for you. DriverInfo类中,您可以有一种方法可以为您执行此操作。

public string GetFormattedTime()
{
   TimeSpan ts = TimeSpan.FromSeconds(_fastestlap);
   return ts.ToString(@"mm\:ss\.fff");

   //shorter version if you prefer
   //return TimeSpan.FromSeconds(_fastestlap).ToString(@"mm\:ss\.fff");
}

MSDN has more information about custom TimeSpan formats. MSDN具有有关自定义TimeSpan格式的更多信息。

Expanding on keyboardP's answer .... 扩展keyboardP的答案 ...。

foreach (XmlNode driver in drivers)
{
  dInfo = new BusObjects.DriverInfo();

  if (driver.ChildNodes[i].Name.Equals("BestLapTime"))
  {
    dInfo.FastestLap = Convert.ToDouble(driver.ChildNodes[i].InnerText);
  }
}

// you can use an auto property for FastestLap
public double FastestLap {get; set;}

// Add another property for FormattedFastestLap:
public string FormattedFastestLap 
{
    get { return TimeSpan.FromSeconds(FastestLap).ToString(@"mm\:ss\.fff"); }
}

In the foreach loop, you are setting the value of the FastestLap, which is a double. foreach循环中,您要设置FastestLap的值,该值是两倍。 By creating another property called FormattedFastestLap, you can access the FastestLap property and convert the double into a string by first converting into a TimeSpan object and then returning the TimeSpan in a formatted string. 通过创建另一个称为FormattedFastestLap的属性,您可以访问FastestLap属性并将双精度型转换为字符串,方法是先转换为TimeSpan对象,然后以格式化的字符串返回TimeSpan。

In your grid, you then bind the display to the FormattedFastestLap instead of the FastestLap property. 然后,在网格中,将显示绑定到FormattedFastestLap而不是FastestLap属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM