oldsport
oldsport

Reputation: 993

How to get values out of XML elements containing a colon

For achieving the above mentioned, I tried all sorts of XNamespace variations. Finally I need the values of the POSITION attributes in a string, with a delimiter: "1|2|3|4"

Code:

XNamespace z = "z";
XNamespace rs = "rs";
var doc = XDocument.Parse(returnValue);
IEnumerable<XElement> list = doc.Root.Descendants(rs +"data");

foreach (var item in list) 
{
    returnValue += item.Element(z + "row").Attribute("POSITION").Value;
    returnValue += "|";
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://schemas/Order">
  <span id="TData">
    <xml pagingEnabled="false" allDataMode="false" rowCount="4">
      <s:Schema xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" id="RowsetSchema">
        <s:ElementType name="row" content="eltOnly">
          <s:AttributeType name="POSITION">
            <s:datatype dt:type="i4" rs:maybenull="false"/>
          </s:AttributeType>
          <s:extends type="rs:rowbase"/>
        </s:ElementType>
      </s:Schema>
      <rs:data xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
        <z:row POSITION="1"/>
        <z:row POSITION="2"/>
        <z:row POSITION="3"/>
        <z:row POSITION="4"/>
      </rs:data>
    </xml>
    <p>
      <form target="_self" id="pickForm" method="post"/>
    </p>
  </span>
</string>

Upvotes: 0

Views: 708

Answers (1)

EZI
EZI

Reputation: 15364

With slight modification to your code

XNamespace rs = "urn:schemas-microsoft-com:rowset"; //<---
XNamespace z = "#RowsetSchema"; //<---
var doc = XDocument.Parse(DATA);

IEnumerable<XElement> list = doc.Root.Descendants(rs + "data");
string returnValue = "";
foreach (var item in list.Elements(z + "row")) //<---
{
    returnValue += item.Attribute("POSITION").Value; //<---
    returnValue += "|";
}

Upvotes: 2

Related Questions