Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在应用字符串操作之前检查null?_C#_.net_Winforms - Fatal编程技术网

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

C# 如何在应用字符串操作之前检查null?,c#,.net,winforms,C#,.net,Winforms,不是每个矩形都有属性转换。 所以我需要以某种方式检查null 如何使用子字符串获取所需的所有字符串? 如果存在属性转换,例如transform=matrix0.98125852,-0.1926959,0.1926959,0.98125852,0,0,那么我需要提取数字:0.98125852,-0.1926959,0.1926959,0.98125852,0,0,并将每个数字放入一个int变量中。问题是,如果最后两个数字是0,0,那么我现在使用子字符串的方式就可以了,但在其他情况下,它们可能是其他

不是每个矩形都有属性转换。 所以我需要以某种方式检查null

如何使用子字符串获取所需的所有字符串? 如果存在属性转换,例如transform=matrix0.98125852,-0.1926959,0.1926959,0.98125852,0,0,那么我需要提取数字:0.98125852,-0.1926959,0.1926959,0.98125852,0,0,并将每个数字放入一个int变量中。问题是,如果最后两个数字是0,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
        });
关于小数点和结果:

decimal.TryParse(collection[i], out decimal result);
及 应为语法错误“,”

和线路上的错误:

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)
        {

        }
    }
}
Invalid expression term 'decimal'
关于结果:

参数1:无法从“字符串”转换为“十进制”

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

results.Add(result);
现在的错误是:


参数2:无法从“输出字符串”转换为“输出小数”

您可以使用三元运算符

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

如果使用C 6.0,则可以使用空条件?。接线员:

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

使用安全导航操作员?:

 Transform= e.Attribute("transform")?.Value.Substring(18, 43),
检查空值 学分:

使用子字符串。 要回答有关如何处理字符串的问题,请尝试以下操作

Transform= e.Attribute("transform")?.Value.Substring(18, 43),
补充资料 根据你的评论。。。这个代码块在这里

0.98125852
-0.1926959
0.1926959
0.98125852
0
0

您不能有字符串结果,然后在其下方再次声明十进制结果。。。我的示例称之为transform

@DanielLip,它与框架版本无关。您仍然可以使用新的编译器。@DanielLip您使用的是哪个版本的visual studio?如果是2014年或更高版本,您应该能够通过进入项目高级设置并提升编译时使用的C版本来完成。你可以用。?对于任何版本的.NET,您只需要VS2014或更高版本。transform始终为空。在这部分之后,我正在对列表进行foreach:列表中的foreach var item{if item.Transform!=null{string Transform=item.Transform;}}}但是item.Transform始终为null,我确信有部分rect具有Transform。@DanielLip太糟糕了,你不能接受两个答案。Haha@DanielLip代码运行良好,请确保您没有对其进行任何编辑。您无法识别十进制结果。我收到错误。若我只做out结果,那个么错误不能从out字符串转换为out十进制。我在foreach循环中添加的所有这部分代码都在列表上循环。用部分代码编辑了我的问题。@DanielLip-我将添加一些额外的代码以帮助您理解。@DanielLip-我认为我的答案现在更适合您的问题。您已将名为string transform的变量更改为string result。。。。这就是它不起作用的原因。。。。因为字符串结果与同一块中的十进制结果冲突,所以x是double,所以使用:double?包含类定义。然后使用X=double?e.Attributex代替value
// 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);
}
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);