Link to home
Start Free TrialLog in
Avatar of dinesh_bali
dinesh_bali

asked on

XPathNavigator in C#

I am using C#

I have instance of XPathNavigator

Now, what is required to me is that

If XPathNavigator.Select("//errors");

Has the values or has the errors than it return no of nodes.

If errors node has no values, then it should return 0.

If XPathNavigator.Select("//errors");

Has the values or it is 1

Can I have the Full XML file in String?

Can anyone check my code for this if it is correct?

especially saving of XPathNavigator to XML as string

I will save this string in database thereafter.

My Code is below:

      public int isErrorExists(XPathNavigator nav)
        {

            XPathNodeIterator iterator = nav.Select("//errors");          

            int cnt = 0;

            while (iterator.MoveNext())
            {
                cnt++;
            }

            if (cnt!= 0)
            {
            string strXML = nav.InnerXml;
            
            // Now save strXML in Database.
          }
      }

Guide me the best way and if my way is correct or not.

Also please correct my code if it is not.

Thanks in Advance

Kind Regards,
Avatar of Gautham Janardhan
Gautham Janardhan

XmlDocument FDocument = new XmlDocument();
FDocument.Load(FFileName);
XmlNodeList FTemp = FDocument.GetElementsByTagName(NodeName);

if(FTemp.Count != 0)
{
string strXML = FDocument.Innerxml;
       //Write to databse
}
i rewrote the code b'coz in ur logic u would have to loop through the iterator which is not necessry as u put it.

ur code will work..
Avatar of dinesh_bali

ASKER

Hi,

I will get XPathNavigator from Object.

Means I will use method which someone else has written will not written me XmlDocument  but it will return me XPathNavigator

So, I want to check the values from XPathNavigator only.

Your Code is using XmlDocument, which is not my requirement.

There for I wrote method, where I will pass XPathNavigator in my function and do the processes.

public int isErrorExists(XPathNavigator nav)

I hope now you are more clear about my requirement and help me better.

Awaiting your reply
Thanks in Advance
public int isErrorExists(XPathNavigator nav)
        {

            XPathNodeIterator iterator = nav.Select("//errors");          


            if (iterator .Count != 0)
            {
          string strXML = nav.InnerXml;
         
          // Now save strXML in Database.
         }
     }
Thanks,

1) If I want all text in the node errors in a string

how we will do this

2) If I want the complete errors node then how we will get it

Many Thanks for your helping
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial