views:

672

answers:

4

Hi, Can someone help me read attribute ows_AZPersonnummer with asp.net using c# from this xml structure

<listitems 
  xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" 
  xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
  xmlns:rs="urn:schemas-microsoft-com:rowset"
  xmlns:z="#RowsetSchema"
  xmlns="http://schemas.microsoft.com/sharepoint/soap/"&gt;
<rs:data ItemCount="1">
   <z:row 
      ows_AZNamnUppdragsansvarig="Peter" 
      ows_AZTypAvUtbetalning="Arvode till privatperson" 
      ows_AZPersonnummer="196202081276"
      ows_AZPlusgiro="5456436534"
      ows_MetaInfo="1;#"
      ows__ModerationStatus="0"
      ows__Level="1" ows_ID="1"
      ows_owshiddenversion="6"
      ows_UniqueId="1;#{11E4AD4C-7931-46D8-80BB-7E482C605990}"
      ows_FSObjType="1;#0"
      ows_Created="2009-04-15T08:29:32Z"
      ows_FileRef="1;#uppdragsavtal/Lists/Uppdragsavtal/1_.000" 
    />
</rs:data>
</listitems>

And get value 196202081276. I have tried all day witout luck

Thanks Lennart

A: 

I'd say you need an XML parser, which I believe are common. This looks like a simple XML structure, so the handling code shouldn't be too hard.

Yuval
+4  A: 

Open this up in an XmlDocument object, then use the SelectNode function with the following XPath:

//*[local-name() = 'row']/@ows_AZPersonnummer

Basically, this looks for every element named "row", regardless of depth and namespace, and returns the ows_AZPersonnummer attribute of it. Should help avoid any namespace issues you might be having.

Welbog
Ok, thanks I will try it tomorrow.
Lennart Stromberg
A: 

Use <%# Eval("path to attribute") %> but you need to load the xml has a DataSource.

Otherwise you can load it using XmlTextReader. Here's an example.

Cedrik
+2  A: 

The XmlNamespaceManager is your friend:

string xml = "..."; //your xml here
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
nsm.AddNamespace("z", "#RowsetSchema");

XmlNode n = doc.DocumentElement
               .SelectSingleNode("//@ows_AZPersonnummer", nsm);
Console.WriteLine(n.Value);

You can also use LINQ to XML:

XDocument xd = XDocument.Parse(xml);
XNamespace xns = "#RowsetSchema";
string result1 = xd.Descendants(xns + "row")
      .First()
      .Attribute("ows_AZPersonnummer")
      .Value;
// Or...

string result2 =
    (from el in xd.Descendants(xns + "row")
     select el).First().Attribute("ows_AZPersonnummer").Value;
Kev
I was about to post the LINQ version [the result1 version], but you're faster! Only difference is I used .FirstOrDefault() and I don't .ToString() at the end (since .Value is a string). The result2 version isn't quite right.
Richard Morgan
Richard...well spotted and appreciated, though I'll leave the First() vs FirstOrDefault() to the OP to decide. :)
Kev
Thanks Kev, it works fine...
Lennart Stromberg