如何在应用字符串操作之前检查null?

时间:2017-06-03 03:51:47

标签: c# .net winforms

var list = document.Root.Descendants(ns + "rect").Select(e => new {
            Style = e.Attribute("style").Value.Substring(16,6),
            Transform= e.Attribute("transform").Value.Substring(18, 43),
            Width = e.Attribute("width").Value,
            Height = e.Attribute("height").Value,
            X = e.Attribute("x").Value
        });

不是每个" rect"拥有属性"转换"。 所以我需要以某种方式检查是否为空。

我如何使用Substring来获取我需要的所有字符串? 如果有属性"转换"例如transform =" matrix(0.98125852,-0.1926959,0.1926959,0.98125852,0,0)"然后我需要提取数字:0.98125852,-0.1926959,0.1926959,0.98125852,0,0并将每个数字放在一个int变量中。问题是,如果最后两个数字是0,0,我现在使用子字符串的方式很好,但在其他情况下它们可能是其他数字。

现在的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace SvgParser
{
    public partial class Form1 : Form
    {
        public enum Rects
        {
            WIDTH, HEIGHT, X, Y, COLOR, ANGLE
        }

        public Form1()
        {
            InitializeComponent();

            ColumnHeader columnHeader1 = new ColumnHeader();
            columnHeader1.Text = "Column1";
            this.listView1.Columns.AddRange(new ColumnHeader[] { columnHeader1 });           
            this.listView1.View = View.Details;

            ParseXml();
        }

        private void ParseXml()
        {
            XDocument document = XDocument.Load(@"C:\Users\mysvg\Documents\my.svg");
            XNamespace ns = "http://www.w3.org/2000/svg";

            var list = document.Root.Descendants(ns + "rect").Select(e => new {
                Style = e.Attribute("style").Value.Substring(16, 6),
                Transform = e.Attribute("transform")?.Value,
                Width = e.Attribute("width").Value,
                Height = e.Attribute("height").Value,
                X = e.Attribute("x").Value
            });

            foreach (var item in list)
            {
                string result = string.Format("Width: {0}, Height: {1}, X: {2}", item.Width, item.Height, item.X);
                if (item.Transform != null)
                {
                    string transform = item.Transform;

                    // figure out the substring parameters
                    int start = "matrix(".Length;
                    int end = transform.LastIndexOf(")") - start;

                    // grab the values, by using split
                    string[] collection = transform.Substring(start, end).Split(',');

                    // an object to hold your results
                    List<decimal> results = new List<decimal>();

                    // iterate through the values (string) and try to convert it to a decimal
                    for (int i = 0; i < collection.Length; i++)
                    {
                        decimal.TryParse(collection[i], out decimal result);
                        results.Add(result);
                    }
                }
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

但我现在得到3个错误。在线:

decimal.TryParse(collection[i], out decimal result);

小数和结果:

Invalid expression term 'decimal'

和     语法错误,&#39;,&#39;预期

错误就行:

results.Add(result);

结果:

参数1:无法转换为&#39; string&#39;到十进制&#39;

然后我将out部分更改为:out result

decimal.TryParse(collection[i], out result);

现在错误是:

参数2:无法转换为&#39; out string&#39;小数&#39;

4 个答案:

答案 0 :(得分:3)

如果使用C#6.0,则可以使用空条件?.运算符:

 Transform= e.Attribute("transform")?.Value.Substring(18, 43),

答案 1 :(得分:3)

检查空

Transform= e.Attribute("transform")?.Value.Substring(18, 43),

信用:https://stackoverflow.com/a/44340111/3645638

使用子字符串。

要回答有关如何处理字符串的问题,请尝试使用

// the string you want to grab the values from
string transform = "matrix(0.98125852,-0.1926959,0.1926959,0.98125852,0,0)";

// figure out the substring parameters
int start = "matrix(".Length;
int end = transform.LastIndexOf(")") - start;

// grab the values, by using split
string[] collection = transform.Substring(start,end).Split(',');

// an object to hold your results
List<decimal> results = new List<decimal>();

// iterate through the values (string) and try to convert it to a decimal
for (int i = 0; i < collection.Length; i++)
{
    decimal.TryParse(collection[i], out decimal result);
    results.Add(result);
}

这将导致名为List<decimal> results,其值为

0.98125852
-0.1926959
0.1926959
0.98125852
0
0

其他信息

根据您的意见......此代码块在此处

for (int i = 0; i < collection.Length; i++)
{
    decimal.TryParse(collection[i], out decimal result);
    results.Add(result);
}

相当于

for (int i = 0; i < collection.Length; i++)
{
    decimal result; // note I am creating a new decimal (defaults to 0)
    decimal.TryParse(collection[i], out result);
    results.Add(result);
}

您的错误

string result = string.Format("Width: {0}, Height: {1}, X: {2}", item.Width, item.Height, item.X);

你不能让string result再次在它下方声明decimal result ...我的例子称之为transform

答案 2 :(得分:2)

您可以使用三元运算符。

Transform = (e.Attribute("transform") != null) ? e.Attribute("transform").Value.Substring(18, 43) : null,

答案 3 :(得分:0)

使用安全导航操作员(?。):

var list = document.Root.Descendants(ns + "rect").Select(e => new {
            Style = e.Attribute("style").Value?.Substring(16,6),
            Transform= e.Attribute("transform").Value?.Substring(18, 43),
            Width = e.Attribute("width").Value,
            Height = e.Attribute("height").Value,
            X = e.Attribute("x").Value
        });