Reading attribute from xml

Can someone help me read the ows_AZPersonnummer attribute 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/">
<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 the value 196202081276.

+3
source share
4 answers

Open this in the object XmlDocument, then use the function SelectNodewith the following XPath:

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

Basically, it searches for every element with the name "string", regardless of depth and namespace, and returns its attribute ows_AZPersonnummer. Any namespace problems that may arise should be avoided.

+6
source

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;
+5

, XML, , , . XML, .

0

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

Otherwise, you can load it using XmlTextReader. Here is an example .

0
source

Source: https://habr.com/ru/post/1708208/


All Articles