DuNuNuBatman
asked on
Getting Values From XmlNode
I'm trying to get certain values from this xml statement, but I keep running into problems. What I'm trying to do is get the ows_Title value, the ows_Description value and the ows_Status value.
There should only be one row returned, but how do I loop through multiple rows and get their values?
The results I have to work with are a XmlNode type.
<listitems xmlns:s="uuid:BDC6E3F0-6DA 3-11d1-A2A 3-00AA00C1 4882" xmlns:dt="uuid:C2F41010-65 B3-11d1-A2 9F-00AA00C 14882" xmlns:rs="urn:schemas-micr osoft-com: rowset" xmlns:z="#RowsetSchema" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<rs:data ItemCount="1">
<z:row ows_Title="Pattie Laptop " ows_Description="Configure and get Pattie the better laptop" ows_Status="Closed" ows_MetaInfo="23;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="23" ows_owshiddenversion="4" ows_UniqueId="23;#{F872192 1-52A6-49F 0-837F-70E 9B7067D9C} " ows_FSObjType="23;#0" ows_Created="2007-09-04T22 :17:23Z" ows_FileRef="23;#clients/a da_es/serv icerequest s/Lists/Se rvice Requests/23_.000" />
</rs:data>
</listitems>
There should only be one row returned, but how do I loop through multiple rows and get their values?
The results I have to work with are a XmlNode type.
<listitems xmlns:s="uuid:BDC6E3F0-6DA
<rs:data ItemCount="1">
<z:row ows_Title="Pattie Laptop " ows_Description="Configure
</rs:data>
</listitems>
another way to read the attributes is
Dim xmldoc As New Xml.XmlDocument
Dim xmlnlist As Xml.XmlNodeList
Dim xmln As Xml.XmlNode
Try
xmldoc.Load("your xml file path")
xmlnlist = xmldoc.SelectNodes("//z:ro w")
For Each xmln In xmlnlist
Dim title As String= xmln.Attributes("ows_Title ").Value
Dim description As String= xmln.Attributes("ows_Descr iption").V alue
Next
Catch ex As Exception
Console.WriteLine(ex.Messa ge)
End Try
Dim xmldoc As New Xml.XmlDocument
Dim xmlnlist As Xml.XmlNodeList
Dim xmln As Xml.XmlNode
Try
xmldoc.Load("your xml file path")
xmlnlist = xmldoc.SelectNodes("//z:ro
For Each xmln In xmlnlist
Dim title As String= xmln.Attributes("ows_Title
Dim description As String= xmln.Attributes("ows_Descr
Next
Catch ex As Exception
Console.WriteLine(ex.Messa
End Try
I'm confused. There should be one row returned, or you need to loop through multiple rows?
Your example shows just one row.
If you know that the Xml Document's Document Element will have one rs:data node then the following code will loop through the z:row nodes.
Note that the XmlDoc object is the Xml Document with the Xml loaded up.
foreach(System.Xml.XmlNode tmpNode in XmlDoc.DocumentElement.Fir stChild)
{
string Title = tmpNode.Attributes.GetName dItem("ows _Title").V alue;
}
Your example shows just one row.
If you know that the Xml Document's Document Element will have one rs:data node then the following code will loop through the z:row nodes.
Note that the XmlDoc object is the Xml Document with the Xml loaded up.
foreach(System.Xml.XmlNode
{
string Title = tmpNode.Attributes.GetName
}
I think he means multiple attributes and not multiple rows
ASKER
No, I meant multiple rows. In this example I only need to get the results of one row. But I needed to know how to get the results of multiple rows. I used your second example because it appears to work for both uses.
I get this error when running your example however. This part is throwing it.
xmlnlist = xmldoc.SelectNodes("//z:ro w")
System.Xml.XPath.XPathExce ption was unhandled by user code
Message="Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."
Source="System.Xml"
StackTrace:
at MS.Internal.Xml.XPath.Comp iledXpathE xpr.get_Qu eryTree()
at System.Xml.XPath.XPathNavi gator.Eval uate(XPath Expression expr, XPathNodeIterator context)
at System.Xml.XPath.XPathNavi gator.Eval uate(XPath Expression expr)
at System.Xml.XPath.XPathNavi gator.Sele ct(XPathEx pression expr)
at System.Xml.XPath.XPathNavi gator.Sele ct(String xpath)
at System.Xml.XmlNode.SelectN odes(Strin g xpath)
at ADADashboard.RequestStatus .GetReques tStatus() in C:\Users\Josh\Desktop\Dash board\Requ estStatus. aspx.cs:li ne 79
at ADADashboard.RequestStatus .Page_Load (Object sender, EventArgs e) in C:\Users\Josh\Desktop\Dash board\Requ estStatus. aspx.cs:li ne 27
at System.Web.Util.CalliHelpe r.EventArg FunctionCa ller(IntPt r fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEvent HandlerDel egateProxy .Callback( Object sender, EventArgs e)
at System.Web.UI.Control.OnLo ad(EventAr gs e)
at System.Web.UI.Control.Load Recursive( )
at System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt)
InnerException:
I get this error when running your example however. This part is throwing it.
xmlnlist = xmldoc.SelectNodes("//z:ro
System.Xml.XPath.XPathExce
Message="Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."
Source="System.Xml"
StackTrace:
at MS.Internal.Xml.XPath.Comp
at System.Xml.XPath.XPathNavi
at System.Xml.XPath.XPathNavi
at System.Xml.XPath.XPathNavi
at System.Xml.XPath.XPathNavi
at System.Xml.XmlNode.SelectN
at ADADashboard.RequestStatus
at ADADashboard.RequestStatus
at System.Web.Util.CalliHelpe
at System.Web.Util.CalliEvent
at System.Web.UI.Control.OnLo
at System.Web.UI.Control.Load
at System.Web.UI.Page.Process
InnerException:
Multiple rows but always just one data node?
Then this should do it
foreach(System.Xml.XmlNode tmpNode in XmlDoc.DocumentElement.Fir stChild)
{
string Title = tmpNode.Attributes.GetName dItem("ows _Title").V alue;
}
Then this should do it
foreach(System.Xml.XmlNode
{
string Title = tmpNode.Attributes.GetName
}
my second example will aloow you to get the results for multiple rows and then loop through them
the error is because of a z: prefix
ASKER
How do I fix that?
try this:
Dim xmldoc As New Xml.XmlDocument
Dim xmlnlist As Xml.XmlNodeList
Dim xmln As Xml.XmlNode
Try
Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(xmldoc .NameTable )
nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform")
xmldoc.Load("your xml file path")
xmlnlist = xmldoc.SelectNodes("//z:ro w")
For Each xmln In xmlnlist
Dim title As String= xmln.Attributes("ows_Title ").Value
Dim description As String= xmln.Attributes("ows_Descr iption").V alue
Next
Catch ex As Exception
Console.WriteLine(ex.Messa ge)
End Try
Dim xmldoc As New Xml.XmlDocument
Dim xmlnlist As Xml.XmlNodeList
Dim xmln As Xml.XmlNode
Try
Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(xmldoc
nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform")
xmldoc.Load("your xml file path")
xmlnlist = xmldoc.SelectNodes("//z:ro
For Each xmln In xmlnlist
Dim title As String= xmln.Attributes("ows_Title
Dim description As String= xmln.Attributes("ows_Descr
Next
Catch ex As Exception
Console.WriteLine(ex.Messa
End Try
ASKER
Here is what I have so far. One of the problem is the results I get from the web service I am calling return a XmlNode type. So I never actually need to create a XmlDoc to add the Node to it.
Lists listService = new Lists();
listService.Credentials = new System.Net.NetworkCredenti al(spUsern ame, spPassword, spDomain);
listService.Url = ConfigSettings.SPListUrl;
// List settings
string listName = ConfigSettings.SPListGuid;
string viewName = null;
string rowLimit = null;
string webID = null;
XmlDocument xmlDoc = new XmlDocument();
// List Query Caml
XmlNode listQuery = xmlDoc.CreateNode(XmlNodeT ype.Elemen t, "Query", "");
listQuery.InnerXml = "<Where>" +
"<Eq>" +
"<FieldRef Name='ID' />" +
"<Value Type='Number'>" + _ServiceRequestID + "</Value>" +
"</Eq>" +
"</Where>";
// List View Caml
XmlNode listViewFields = xmlDoc.CreateNode(XmlNodeT ype.Elemen t, "ViewFields", "");
listViewFields.InnerXml = "<FieldRef Name='Title' />" +
"<FieldRef Name='Description' />" +
"<FieldRef Name='Status' />" +
"<FieldRef Name='Resolution' />";
// List Query Options Caml
XmlNode listQueryOptions = xmlDoc.CreateNode(XmlNodeT ype.Elemen t, "QueryOptions", "");
listQueryOptions.InnerXml = "<IncludeMandatoryColumns> FALSE</Inc ludeMandat oryColumns >" +
"<DateInUtc>TRUE</DateInUt c>";
XmlNode results = listService.GetListItems(l istName, viewName, listQuery, listViewFields, rowLimit, listQueryOptions, webID);
XmlNodeList nodeList = results.SelectNodes("//z:r ow");
foreach (XmlNode node in nodeList)
{
Response.Write(node.Attrib utes["ows_ Title"].In nerText);
Response.Write(node.Attrib utes["ows_ Descriptio n"].InnerT ext);
Response.Write(node.Attrib utes["ows_ Status"].I nnerText);
}
Response.Write("<br />");
Response.Write(Server.Html Encode(res ults.Outer Xml));
Lists listService = new Lists();
listService.Credentials = new System.Net.NetworkCredenti
listService.Url = ConfigSettings.SPListUrl;
// List settings
string listName = ConfigSettings.SPListGuid;
string viewName = null;
string rowLimit = null;
string webID = null;
XmlDocument xmlDoc = new XmlDocument();
// List Query Caml
XmlNode listQuery = xmlDoc.CreateNode(XmlNodeT
listQuery.InnerXml = "<Where>" +
"<Eq>" +
"<FieldRef Name='ID' />" +
"<Value Type='Number'>" + _ServiceRequestID + "</Value>" +
"</Eq>" +
"</Where>";
// List View Caml
XmlNode listViewFields = xmlDoc.CreateNode(XmlNodeT
listViewFields.InnerXml = "<FieldRef Name='Title' />" +
"<FieldRef Name='Description' />" +
"<FieldRef Name='Status' />" +
"<FieldRef Name='Resolution' />";
// List Query Options Caml
XmlNode listQueryOptions = xmlDoc.CreateNode(XmlNodeT
listQueryOptions.InnerXml = "<IncludeMandatoryColumns>
"<DateInUtc>TRUE</DateInUt
XmlNode results = listService.GetListItems(l
XmlNodeList nodeList = results.SelectNodes("//z:r
foreach (XmlNode node in nodeList)
{
Response.Write(node.Attrib
Response.Write(node.Attrib
Response.Write(node.Attrib
}
Response.Write("<br />");
Response.Write(Server.Html
ASKER
which one am I looking for?
this will work:
Dim xmldoc As New Xml.XmlDocument
Dim xmlnlist As Xml.XmlNodeList
Dim xmln As Xml.XmlNode
Try
xmldoc.Load("your xml file path")
'Since the XML returned by SPS contains namespace prefixes - we need to create a XmlNamespaceManager
Dim ListItemsNamespacePrefix As String = "z"
Dim ListItemsNamespaceURI As String = "#RowsetSchema"
Dim PictureLibrariesNamespaceP refix As String = "s"
Dim PictureLibrariesNamespaceU RI As String = "uuid:BDC6E3F0-6DA3-11d1-A 2A3-00AA00 C14882"
Dim WebPartsNamespacePrefix As String = "dt"
Dim WebPartsNamespaceURI As String = "uuid:C2F41010-65B3-11d1-A 29F-00AA00 C14882"
Dim DirectoryNamespacePrefix As String = "rs"
Dim DirectoryNamespaceURI As String = "urn:schemas-microsoft-com :rowset"
'now associate with the xmlns namespaces (part of all XML nodes returned
'from SharePoint) a namespace prefix which we can then use in the queries
Dim nsmgr As Xml.XmlNamespaceManager = New Xml.XmlNamespaceManager(xm ldoc.NameT able)
nsmgr.AddNamespace(ListIte msNamespac ePrefix, ListItemsNamespaceURI)
nsmgr.AddNamespace(Picture LibrariesN amespacePr efix, PictureLibrariesNamespaceU RI)
nsmgr.AddNamespace(WebPart sNamespace Prefix, WebPartsNamespaceURI)
nsmgr.AddNamespace(Directo ryNamespac ePrefix, DirectoryNamespaceURI)
xmlnlist = xmldoc.SelectNodes("//z:ro w", nsmgr)
For Each xmln In xmlnlist
Dim title As String = xmln.Attributes("ows_Title ").Value
Dim description As String = xmln.Attributes("ows_Descr iption").V alue
Next
Catch ex As Exception
Console.WriteLine(ex.Messa ge)
End Try
Dim xmldoc As New Xml.XmlDocument
Dim xmlnlist As Xml.XmlNodeList
Dim xmln As Xml.XmlNode
Try
xmldoc.Load("your xml file path")
'Since the XML returned by SPS contains namespace prefixes - we need to create a XmlNamespaceManager
Dim ListItemsNamespacePrefix As String = "z"
Dim ListItemsNamespaceURI As String = "#RowsetSchema"
Dim PictureLibrariesNamespaceP
Dim PictureLibrariesNamespaceU
Dim WebPartsNamespacePrefix As String = "dt"
Dim WebPartsNamespaceURI As String = "uuid:C2F41010-65B3-11d1-A
Dim DirectoryNamespacePrefix As String = "rs"
Dim DirectoryNamespaceURI As String = "urn:schemas-microsoft-com
'now associate with the xmlns namespaces (part of all XML nodes returned
'from SharePoint) a namespace prefix which we can then use in the queries
Dim nsmgr As Xml.XmlNamespaceManager = New Xml.XmlNamespaceManager(xm
nsmgr.AddNamespace(ListIte
nsmgr.AddNamespace(Picture
nsmgr.AddNamespace(WebPart
nsmgr.AddNamespace(Directo
xmlnlist = xmldoc.SelectNodes("//z:ro
For Each xmln In xmlnlist
Dim title As String = xmln.Attributes("ows_Title
Dim description As String = xmln.Attributes("ows_Descr
Next
Catch ex As Exception
Console.WriteLine(ex.Messa
End Try
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome, that's exactly what I was looking for. Here is my final code for reference.
// Authentication settings
string spUsername = ConfigSettings.SPUsername;
string spPassword = ConfigSettings.SPPassword;
string spDomain = ConfigSettings.SPDomain;
Lists listService = new Lists();
listService.Credentials = new System.Net.NetworkCredenti al(spUsern ame, spPassword, spDomain);
listService.Url = ConfigSettings.SPListUrl;
// List settings
string listName = ConfigSettings.SPListGuid;
string viewName = null;
string rowLimit = null;
string webID = null;
XmlDocument xmlDoc = new XmlDocument();
// List Query Caml
XmlNode listQuery = xmlDoc.CreateNode(XmlNodeT ype.Elemen t, "Query", "");
listQuery.InnerXml = "<Where>" +
"<Eq>" +
"<FieldRef Name='ID' />" +
"<Value Type='Number'>" + _ServiceRequestID + "</Value>" +
"</Eq>" +
"</Where>";
// List View Caml
XmlNode listViewFields = xmlDoc.CreateNode(XmlNodeT ype.Elemen t, "ViewFields", "");
listViewFields.InnerXml = "<FieldRef Name='Title' />" +
"<FieldRef Name='Description' />" +
"<FieldRef Name='Status' />" +
"<FieldRef Name='Resolution' />";
// List Query Options Caml
XmlNode listQueryOptions = xmlDoc.CreateNode(XmlNodeT ype.Elemen t, "QueryOptions", "");
listQueryOptions.InnerXml = "<IncludeMandatoryColumns> FALSE</Inc ludeMandat oryColumns >" +
"<DateInUtc>TRUE</DateInUt c>";
XmlNode results = listService.GetListItems(l istName, viewName, listQuery, listViewFields, rowLimit, listQueryOptions, webID);
XmlNamespaceManager spsNameSpaces = new XmlNamespaceManager(xmlDoc .NameTable );
spsNameSpaces.AddNamespace ("s", "uuid:BDC6E3F0-6DA3-11d1-A 2A3-00AA00 C14882");
spsNameSpaces.AddNamespace ("dt", "uuid:C2F41010-65B3-11d1-A 29F-00AA00 C14882");
spsNameSpaces.AddNamespace ("rs", "urn:schemas-microsoft-com :rowset");
spsNameSpaces.AddNamespace ("z", "#RowsetSchema");
spsNameSpaces.AddNamespace ("", "http://schemas.microsoft.com/sharepoint/soap/");
XmlNodeList nodeList = results.SelectNodes("//z:r ow", spsNameSpaces);
foreach (XmlNode node in nodeList)
{
lblTitle.Text = node.Attributes["ows_Title "].InnerTe xt;
lblDescription.Text = node.Attributes["ows_Descr iption"].I nnerText;
lblStatus.Text = node.Attributes["ows_Statu s"].InnerT ext;
}
// Authentication settings
string spUsername = ConfigSettings.SPUsername;
string spPassword = ConfigSettings.SPPassword;
string spDomain = ConfigSettings.SPDomain;
Lists listService = new Lists();
listService.Credentials = new System.Net.NetworkCredenti
listService.Url = ConfigSettings.SPListUrl;
// List settings
string listName = ConfigSettings.SPListGuid;
string viewName = null;
string rowLimit = null;
string webID = null;
XmlDocument xmlDoc = new XmlDocument();
// List Query Caml
XmlNode listQuery = xmlDoc.CreateNode(XmlNodeT
listQuery.InnerXml = "<Where>" +
"<Eq>" +
"<FieldRef Name='ID' />" +
"<Value Type='Number'>" + _ServiceRequestID + "</Value>" +
"</Eq>" +
"</Where>";
// List View Caml
XmlNode listViewFields = xmlDoc.CreateNode(XmlNodeT
listViewFields.InnerXml = "<FieldRef Name='Title' />" +
"<FieldRef Name='Description' />" +
"<FieldRef Name='Status' />" +
"<FieldRef Name='Resolution' />";
// List Query Options Caml
XmlNode listQueryOptions = xmlDoc.CreateNode(XmlNodeT
listQueryOptions.InnerXml = "<IncludeMandatoryColumns>
"<DateInUtc>TRUE</DateInUt
XmlNode results = listService.GetListItems(l
XmlNamespaceManager spsNameSpaces = new XmlNamespaceManager(xmlDoc
spsNameSpaces.AddNamespace
spsNameSpaces.AddNamespace
spsNameSpaces.AddNamespace
spsNameSpaces.AddNamespace
spsNameSpaces.AddNamespace
XmlNodeList nodeList = results.SelectNodes("//z:r
foreach (XmlNode node in nodeList)
{
lblTitle.Text = node.Attributes["ows_Title
lblDescription.Text = node.Attributes["ows_Descr
lblStatus.Text = node.Attributes["ows_Statu
}
Dim xtr As XmlTextReader
xtr = New XmlTextReader("your xml file path")
xtr.WhiteSpaceHandling = WhiteSpaceHandling.NONE
xtr.Read()
xtr.Read()
While Not xtr.EOF
xtr.Read()
If Not xtr.IsStartElement() Then
Exit While
End If
'Get the Gender Attribute Value
Dim title as string = xtr.GetAttribute("ows_Titl
End While
''close the reader
xtr.Close()