score:0
Accepted answer
This may help you. Everything you wrote that code was right, you just rather use main.Element("Network").Attributes()
than main.Attributes()
IEnumerable<XAttribute> successAttributes =
from attribute in main.Element("Network").Attributes()
where attribute.Name.LocalName == "Nid"
select attribute;
On behalf of this issue I wrote the below sample program which will yield the expected NID value
string strVal = "<Server><Network Nid=\"43d5377-0dcd-40e6-b95c-8ee980b1e248\"/><Client_Group id=\"20\">963440d0-96dc-46a4-a54d-7251a65f585f</Client_Group><ClientID id=\"20\">3fc8ffa1-c16b-4d7b-9e55-1e88dfe15277</ClientID></Server>";
XElement main = XElement.Load(new StringReader(strVal));
IEnumerable<XAttribute> successAttributes =
from attribute in main.Element("Network").Attributes()
where attribute.Name.LocalName == "Nid"
select attribute;
score:0
main
here is the root element: <Server>
- which does not have a Nid
attribute.
You want something like:
Guid nid = (Guid)main.Element("Network").Attribute("Nid");
Or for multiple:
Guid[] arr = (from attr in main.DescendentsOrSelf().Attributes("Nid")
select (Guid)attr).ToArray();
Source: stackoverflow.com
Related Articles
- IEnumerable<XAttribute> is returning Null, Needs to Return Attribute Value
- Return null instead default value in LINQ
- LINQ - Returning value of a property of an object that is not null
- Visual Studio assumes return value from FirstOrDefaultAsync() cannot be null
- SQL Trigger is trying to insert a null value but my C# code is passing int 300?
- default(T?) does not return null when T is a value type
- Return cell with value not null using linq - Epplus
- Need to join two strings if both contain values, or return one value if first is NULL
- Linq 2 XML not returning element based in attribute value
- returning default value if a property is null
- Returning value on null linq query
- Show some default text if return value is null or empty
- Linq query return coumn value null handle
- LINQ function to return list but compiler says function doesn't return a value on all code path
- How to search a class within a namespace by the value of an attribute and return an object?
- Simple Linq expression Sum - Not returning null value for empty list
- Error 1 'Form1.CountWordsInstances(string, string)': not all code paths return a value
- Return value of stored procedure is correct but column values are all NULL
- Return a value or null using LINQ in one line?
- Linq to XML Find a an attribute and return seperate attribute value
- LINQ returning Null while same query when write in SQL return result
- c# Null value returning from Linq
- How to return null value when using CopyToDataTable() LINQ extension method
- How do I return list of elements by attribute value
- Return value or null, without null reference exception linq
- Iterate through elements of the same name and return attribute by another attribute value
- How to return all values of a column as null except one column value in LINQ?
- Select Linq statement needs to return 0 if either item is null
- where Clause in Asp.net is returning null exception in the code below
- Linq query returning null when trying to pass a column value from list object
- Linq to entities DbQuery object is failing hard
- LINQ-to-SQL vs stored procedures?
- How to implement functional object construction in C#
- What's elegant way to rewrite following LINQ statement using extension methods?
- Find path to subnodes of treeview using linq. (Completely flattening a treeview)
- How does LINQ Where() clause in .Net work?
- Nested query results in LINQ (or sub queries)
- Linq exception when join a list on a query result
- How can I round" truncate" my grade to one decimal in LINQ?
- How can I convert this console mongodb query to a C# mongo driver v2 one?
- for loop in where clause
- XML node Latest timestamp C#
- How to handle Null array in Linq
- Checking date range against set of date ranges in a data table
- Select one row per specific time
- Reading a delimted file using LINQ
- Reusable Anonymous Ranking Function For Composite Class
- Select only few columns from a table to map with Odata Select query on C# Web API
- Duplicates while querying Three tables to get Common values using LINQ.
- Compiled C# lambda expression performance with imbrication