I am attempting to create a loop to read through each XML attribute in a node and put it into a datatable. I have created my datatable with columns that are the same name as the attributes in my XML. I keep getting an error which says "Unable to cast object of type 'System.Xml.Xml Element' to type 'System.Xml.Xml Attribute'." The code follows:
The XML I am trying to read is:
If you can't tell already, this is from a SharePoint query. I am not a developer, but I'm having fun playing around with .NET and creating forms to make my SharePoint more accessible. I appreciate any help people can give, and I look forward to replies.
Moderator: Please move this to the C# Forum, I did not realize there was a better place to post. done. --insertAlias
Code:
foreach (XmlNode listItem in newBatch) { if (listItem.NodeType == XmlNodeType.Element) { foreach (XmlNode subNode in listItem) { string XMLtoREAD = subNode.OuterXml; XmlDocument AttribXML = new XmlDocument(); AttribXML.LoadXml(XMLtoREAD); //Find datatable DataTable Items = ROV; DataRow dr; dr = Items.NewRow(); //Open Xml to read attributes XmlTextReader reader = new XmlTextReader(new StringReader(XMLtoREAD)); reader.WhitespaceHandling = System.Xml.WhitespaceHandling.None; reader.Read(); foreach (XmlAttribute attrib in AttribXML) { string AttName = attrib.ToString(); reader.MoveToAttribute(AttName); dr[AttName] = reader.Value; MessageBox.Show(reader.Value); } Items.Rows.Add(dr); Items.AcceptChanges(); reader.Close(); } } }
Code:
<rs:data ItemCount="1" xmlns:rs="urn:schemas-microsoft-com:rowset"> <z:row ows_Date="2010-05-12 00:00:00" ows_Title="9" ows_Serial_x0020_Number="258" ows_Invoice_x0020_Number="254" ows_Supplier="synnex" ows_SKU="25365" ows_Manufacturer="8" ows_Description="15" ows_Qty_x002e_="1" ows_Price="235" ows_Freight="100" ows_Customer="as" ows_Total_x0020_Price="45" ows_ID="58" ows__ModerationStatus="0" ows__Level="1" ows_owshiddenversion="1" ows_UniqueId="58;#{88D9AAB5-61A9-46F7-80A4-DDBCA8C39475}" ows_FSObjType="58;#0" ows_Created="2010-05-12 11:49:01" ows_FileRef="58;#Calls/Lists/Parts/58_.000" ows_MetaInfo="58;#" xmlns:z="#RowsetSchema" /> </rs:data>
Moderator: Please move this to the C# Forum, I did not realize there was a better place to post. done. --insertAlias
Comment