1

My XML file look like this:

<?xml version="1.0" encoding="utf-8"?>
<dskh>
  <khachhang maso="kh01">
    <ten_kh>nhi</ten_kh>
    <tuoi_kh>15</tuoi_kh>
    <dchi_kh>dt</dchi_kh>
  </khachhang>

  <khachhang maso="kh02">
    <ten_kh>hung</ten_kh>
    <tuoi_kh>15</tuoi_kh>
    <dchi_kh>hcm</dchi_kh>
  </khachhang>
</dskh>

I want to get last attribute value of khachhang element. In this case is maso="kh02". Because i want when i insert new khachhang element, maso attribute will increase auto. Someone can advice me some good ...Thank everyone so much!

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239

2 Answers2

1

Alternatively

var xdoc = XDocument.Load(path_to_xml);
var lastElement = xdoc.Root.Elements("khachhang").Last();
var value = lastElement.Attribute("maso").Value;

Note I always put null checks in between accessing an element or attributes value that I have just read, just in case.

RobJohnson
  • 875
  • 11
  • 21
0
var xdoc = XDocument.Load(path_to_xml);
var result = (string)xdoc.XPathSelectElement("//khachhang[last()]")
                         .Attribute("maso");

Or

var result = (string)xdoc.Descendants("khachhang").Last()
                         .Attribute("maso");

Also if it is possible that no khachhang elements will be in your xml, you should try to get last element. And then get attribute if khachhang element was found:

var lastKhachhang = xdoc.Descendants("khachhang").LastOrDefault();
if (lastKhachhang == null)
    // use default maso value
else
   maso = (string)lastKhachhang.Attribute("maso");

It's important to use LastOrDefault in this case. Otherwise you'll get an exception.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • What about **maso** auto increase, can you help me? – user1621647 Jan 18 '13 at 13:24
  • when i learn Linq to XML..I see this code: var q = from c in loaded.Descendants("contact") where (int)c.Attribute("contactId") < 4 select (string)c.Element("firstName") + “ “ + (string)c.Element("lastName"); how can i write like here bro: var q = from c in xmlSource.contact where c.contactId < 4 select c.firstName + " " + c.lastName; – user1621647 Jan 18 '13 at 13:59
  • @user1621647 sorry, didn't get what you are trying to do. First sample is Linq to Xml. Second is something else (possibly Linq to Objects) – Sergey Berezovskiy Jan 18 '13 at 14:03
  • can you explain me what's different between last and lastordefault method? – user1621647 Jan 22 '13 at 16:20
  • @user1621647 sure, if sequence does not contain elements, then `Last()` will throw an exception. `LastOrDefault()` in this case will just return default value for sequence element type (null for XElement). So, if there is no `khachhang` elements (i.e. before you will add first one), you will get exception with `Last()` method – Sergey Berezovskiy Jan 22 '13 at 16:28
  • hi bro, i want encrypt and hide xml source file. What should i do? – user1621647 Jan 23 '13 at 09:14
  • when i decrypt xml file, it through exception: Bad Data. What does it mean, bro? – user1621647 Jan 23 '13 at 11:58
  • @user1621647 sorry, I'm kind of busy now. You can ask a question on SO :) – Sergey Berezovskiy Jan 23 '13 at 12:37