Solved

XPathNavigator in C#

Posted on 2006-11-17
9
653 Views
Last Modified: 2012-06-27
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,
0
Comment
Question by:dinesh_bali
  • 4
  • 2
9 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17964350
XmlDocument FDocument = new XmlDocument();
FDocument.Load(FFileName);
XmlNodeList FTemp = FDocument.GetElementsByTagName(NodeName);

if(FTemp.Count != 0)
{
string strXML = FDocument.Innerxml;
       //Write to databse
}
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17964356
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..
0
 

Author Comment

by:dinesh_bali
ID: 17964403
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
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17964494
public int isErrorExists(XPathNavigator nav)
        {

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


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

Author Comment

by:dinesh_bali
ID: 17964620
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
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 17964668
public int isErrorExists(XPathNavigator nav)
        {

            XPathNodeIterator iterator = nav.Select("//errors");
          string strXML = "";
          if (iterator.Count!= 0)
          {  
            while (iterator.MoveNext())
            {
                strXml +=  iterator.Current.InnerXml;
            }    
          // Now save strXML in Database.
         }
     }

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now