Reputation: 11325
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(15, 7),
Transform = e.Attribute("transform")?.Value,
Width = e.Attribute("width").Value,
Height = e.Attribute("height").Value,
X = e.Attribute("x").Value
});
In csharp it's working fine. But in unity visual studio i'm getting error on the line:
e.Attribute("transform")?.Value.Substring(18, 43)
Feature 'null propagating operator' is not available in C# 4. Please use language version 6 or greater.
In csharp i didn't have to change anything.
The visual studio i'm using in unity (same as for csharp) is: 14.0.24531.01 Update 3 and visual c# 2015
Maybe i need to change the line checking for null for something else ?
Upvotes: 0
Views: 456
Reputation: 125435
You already know why you can't use ?.
and that's because Unity does not support that version of C# that supports ?.
.
UnholySheep comment suggests using if statement but I don't think you can use that here.
You can check for null
with the ternary operator.
Use:
Transform = e.Attribute("transform") != null ? e.Attribute("transform").Value : "",
If you are still confused. Here is the whole code:
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(15, 7),
Transform = e.Attribute("transform") != null ? e.Attribute("transform").Value : "",
Width = e.Attribute("width").Value,
Height = e.Attribute("height").Value,
X = e.Attribute("x").Value
});
Upvotes: 1
Reputation: 35318
You can do it the old fashioned way with a ternary operator ?:
, and I'd get the attribute outside the collection initializer so you don't have to index it twice:
var list = document.Root.Descendants(ns + "rect").Select(e =>
var tr = e.Attribute("transform");
new {Style = e.Attribute("style").Value.Substring(15, 7),
Transform = (tr != null) ? tr.Value : null,
Width = e.Attribute("width").Value,
Height = e.Attribute("height").Value,
X = e.Attribute("x").Value
});
If you upgrade your version of Visual Studio to 2015 or 2017, you can use the null conditional operator.
Upvotes: 0