私はRect.rectangles内の文字列「マトリックス(0.87142591,0.49052715、-0.49052715,0.87142591,0,0)」2つの変換などのプロパティとtransform1を持っているし、それぞれの番号を持つことになりますを持っている代わりに、transformプロパティを下にしたいです例えば:私は部分文字列を使用しようとしているが、範囲例外のうち、取得してい文字列の数値だけを解析するにはどうすればよいですか?
public class Rect
{
public static List<Rect> rectangles { get; set; }
public Dictionary<string, string> style { get; set; }
public string id { get; set; }
public double width { get; set; }
public double height { get; set; }
public double? x { get; set; }
public double? y { get; set; }
public string transform { get; set; }
public static Dictionary<string, string> GetStyle(string styles)
{
string pattern = @"(?'name'[^:]+):(?'value'.*)";
string[] splitArray = styles.Split(new char[] { ';' });
Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern))
.GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
return style;
}
}
: "0.12345678" transform1 "1.12345678"
private void Parse()
{
XDocument document = XDocument.Load(@"C:\Users\mysvg\Documents\my.svg");
Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
{
style = Rect.GetStyle((string)x.Attribute("style")),
id = (string)x.Attribute("id"),
width = (double)x.Attribute("width"),
height = (double)x.Attribute("width"),
x = (double?)x.Attribute("width"),
y = (double?)x.Attribute("width"),
transform = x.Attribute("transform".Substring(18, 43)) == null ? "" : (string)x.Attribute("transform".Substring(18, 43))
}).ToList();
}
とのRectクラスを変換します。
例外ArgumentOutOfRangeException:行で文字列
の長さを超えることはできません:
transform = x.Attribute("transform".Substring(18, 43)) == null ? "" : (string)x.Attribute("transform".Substring(18, 43))
文字列がある:
は、= "マトリックス(0.40438612、-0.91458836,0.92071162,0.39024365変換0,0)」
と私は上取得したいですLY最初の2つの数字:0.40438612、-0.91458836
更新:
private void Parse()
{
XDocument document = XDocument.Load(@"C:\Users\mysvg\Documents\my.svg");
Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
{
style = Rect.GetStyle((string)x.Attribute("style")),
id = (string)x.Attribute("id"),
width = (double)x.Attribute("width"),
height = (double)x.Attribute("width"),
x = (double?)x.Attribute("width"),
y = (double?)x.Attribute("width"),
transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform")
}).ToList();
string t = null;
foreach(Rect rect in Rect.rectangles)
{
if (rect.transform != null && rect.transform != "")
{
t = rect.transform.Substring(7, 21);
}
}
}
しかし、私は新しいの後にそれを行うにはしたくない:
私はまたのforeachを使用してそれを行うための方法を見つけましたRECT(){ 私はライン上の最初の2つの数字の抽出をしたい:
transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform")
理由は、私は、コードクラスの下に持っているということです。
public class Rect
{
public static List<Rect> rectangles { get; set; }
public Dictionary<string, string> style { get; set; }
public string id { get; set; }
public double width { get; set; }
public double height { get; set; }
public double? x { get; set; }
public double? y { get; set; }
public string transform { get; set; }
public static Dictionary<string, string> GetStyle(string styles)
{
string pattern = @"(?'name'[^:]+):(?'value'.*)";
string[] splitArray = styles.Split(new char[] { ';' });
Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern))
.GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
return style;
}
}
そして、私は、プロパティの変換がすでに2つの数値を保持/保持したいと思っています。
もう1つの小さな問題は、最初の2つの数値を抽出するにはどうすればよいのでしょうか?たとえば、最初の2つの数値に - (マイナス) 、0,0)」
2つの数の1が開始またはそれらの両方持っているマイナスサブストリング(7、21)が動作しません起動にマイナス持っている場合。私は何とか最初の2つの数字を取得する必要があります。しかし、主な問題は、属性から直接数値を抽出する方法です。
最良の答えですpositiion 18で始まり、9文字の長さの文字列 '' transform "'から長さ43を持ちます。 –