Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
xml file is like as below...

i want to get two data 'user_name' and 'email'

using xpathnavigator in c#.......................but can't... result is nothing...

------------------------------------------------------------------------------------------
XML
<?xml version="1.0" encoding="UTF-8"?>

<MSG xmlns="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <RD>
    <xs:schema xmlns="" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="NewDataSet">
      <xs:element msdata:UseCurrentLocale="true" msdata:IsDataSet="true" name="NewDataSet">
        <xs:complexType>
          <xs:choice maxOccurs="unbounded" minOccurs="0">
            <xs:element name="Table" msprop:BaseTable.0="PATMAST_VIEW">
              <xs:complexType>
                <xs:sequence>
                   <xs:element name="USERNAME" minOccurs="0" type="xs:string" msprop:BaseColumn="USERNAME" msprop:OraDbType="126"/>
                   <xs:element name="EMAIL" minOccurs="0" type="xs:decimal" msprop:OraDbType="107"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:schema>

    <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
      <NewDataSet xmlns="">
        <Table diffgr:id="Table1" msdata:rowOrder="0">
          <USERNAME>Jim</USERNAME>
          <EMAIL>1</EMAIL>
      </Table>
      </NewDataSet>
    </diffgr:diffgram>

  </RD>

</MSG >


What I have tried:

...read xml...
XPathNavigator xpN = xDoc.CreateNavigator();
XPathExpression expression = xpN.Compile('MSG/RD/diffgr:diffgram/NewDataSet/Table/USERNAME');
XmlNamespaceManager manager = new XmlNamespaceManager(xpN.NameTable);
manager.AddNamespace("diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1");
expression.SetContext(manager);
XPathNodeIterator iter = xpN.Select(expression);
if (iter.Count <= 0) continue;
......
Posted
Updated 26-May-16 3:11am

1 solution

Alternative way to do it.
Using a DataSet will automatically give you the table structure.
C#
XDocument xDoc = XDocument.Load('your file name');
XElement xeMsg = xDoc.Element(XName.Get("MSG", "http://tempuri.org/"));
XElement xeRd = xeMsg.Element(XName.Get("RD", "http://tempuri.org/"));

DataSet ds = new DataSet();
ds.ReadXml(xeRd.CreateReader());

if (ds.Tables["Table"].Rows.Count == 1)
{
    DataRow dr = ds.Tables["Table"].Rows[0];

    string userName = dr["USERNAME"].ToString();
    string email = dr["EMAIL"].ToString();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900