Advertisement
IzaacJ

LINQ to XML in C# error?

Nov 6th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. // This returns AccessViolationException. There is an element named "account" with the attributes "title" and "uname" set, according to http://msdn.microsoft.com/en-us/library/bb348975(v=vs.110).aspx it should work!?
  2.  
  3. // Sample XML content
  4. // <xml>
  5. // <settings>
  6. //    <setting name="test" value="value"/>
  7. // </settings>
  8. // <accounts>
  9. //    <account title="test" uname="testname"/>
  10. // </accounts>
  11. // </xml>
  12.  
  13. IEnumerable<XElement> tmpSettings = null;
  14. IEnumerable<XElement> tmpAccounts = null;
  15. try
  16. {
  17.    tmpSettings = from el in XMLData.Elements("setting")
  18.                  select el;
  19.    System.Diagnostics.Debug.WriteLine("Node name: " + tmpSettings.FirstOrDefault().Attribute("name").Value);
  20.    System.Diagnostics.Debug.WriteLine("Node value: " + tmpSettings.FirstOrDefault().Attribute("value").Value);
  21.    tmpAccounts = from el in XMLData.Elements("account")
  22.                  select el;
  23.    System.Diagnostics.Debug.WriteLine("Node name: " + tmpAccounts.FirstOrDefault().Attribute("title").Value);
  24.    System.Diagnostics.Debug.WriteLine("Node value: " + tmpAccounts.FirstOrDefault().Attribute("uname").Value);
  25. }catch(Exception ex)
  26. {
  27.    //do nothing
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement