Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi All,

 

I'm iterating through an XML document, using C# .NET. I need to save specific nodes on the fly....to do later searches on those nodes. How can I save these nodes?? XmlNodeList doens't contain an "Add" method...and ArrayList doesn't support 'XmlNode' type objects. Any suggestions ???? Please help.

 

--Matt

Posted

Hi!

 

You didn't specify what kind of work you're doing but, latelly I've been doind a lot of work in XML and what I do mostly is serialize a typed-collection.

This way you add/remove or reorder the collection items as .net System.Collections nampespace well does and thes just serialize the collection itself.

 

This way you can either serach for nodes in XML with XPath or deserialize the XML document back to its typed-collection form and work with the data there.

 

Just note that I'm talking about typed-collections but in MSDN I read about the ability of .net serialize untyped-collection due the use of some parameters on the array but I never managed to do that...

 

By the way... typed and untyped collections are collections declared in VB like:

'Typed
Dim TextBoxes() as System.Windows.Forms.TextBox

'Untyped
Dim Objects as new ArrayList

 

Alex :p

Software bugs are impossible to detect by anybody except the end user.
Posted

Hey Alex,

 

Thanks for reply. Let me explain a little more. By the way, I'm using C#, not VB. Is there anyway you could include your examples in C#? Thanks.

 

I've got an XmlNodeList. I run a query, and pull out specific nodes from the XmlNodeList that match the query statement. I need to create a new XmlNodeList from these selected nodes so that I can pass this new XmlNodeList into another function. I hope you can help. This will REALLY solve a huge problem for me. I appreciate it Alex.

 

--Matt

Posted

Hi... I don't know if you already have the query part figured out but here it goes:

private const String localURL = "http://localhost/quickstart/howto/samples/Xml/QueryXmlDocumentXPath/cs/books.xml";

public static void Main()
{
   QueryXmlDocumentXPathSample myQueryXmlDocumentXPathSample = new QueryXmlDocumentXPathSample();
   myQueryXmlDocumentXPathSample.Run(localURL);
}

public void Run(String args)
{
   Console.WriteLine("XPath Test started ...");

   XPathDocument myXPathDocument = new XPathDocument(args);
   XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();

   // Get all the book prices
   XPathQuery(myXPathNavigator, "descendant::book/price");

   // Get the ISBN of the last book
   XPathQuery(myXPathNavigator, "bookstore/book[3]/@ISBN");
}

private void XPathQuery(XPathNavigator myXPathNavigator, String xpathexpr )
{
   try
   {
       Console.WriteLine("XPath query: " + xpathexpr);

       // Create a node interator to select nodes and move through them (read-only)
       XPathNodeIterator myXPathNodeIterator =  myXPathNavigator.Select (xpathexpr);

       while (myXPathNodeIterator.MoveNext())
       {
           Console.WriteLine("<" + myXPathNodeIterator.Current.Name + "> " + myXPathNodeIterator.Current.Value);
       }
       Console.WriteLine();
   }
   catch (Exception e)
   {
       Console.WriteLine ("Exception: {0}", e.ToString());
   }
}

 

The query can also be performed using an XPathExpression class . There are two main reasons for using the XPathExpression class over using the string expression in the S elect method (on the XPathNavigator) :

1) The expression is compiled only once and can be reused several times thus improving performance

2) Prefixes (s) used in the XPath expression can be bound to a namespace using a XmlNamespaceManager class - this allows for prefixed XPath expressions when selecting a set of nodes.

The following code conveys the general usage which selects all the book whose ISBN attribute are in the urn:samples namespace.

 

// compile the expression
XPathExpression expr = nav.Compile("book/@mybk:ISBN");
// set namespace(s) used in the expression
XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
mngr.AddNamespace("mybk","urn:samples");
expr.SetContext(mngr);
// select the nodes
XPathNodeIterator myXPathNodeIterator = nav.Select(expr);


' compile the expression
Dim expr as XPathExpression = nav.Compile("book/@mybk:ISBN")
' set namespace(s) used in the expression
Dim mngr as XmlNamespaceManager = new XmlNamespaceManager(new NameTable())
mngr.AddNamespace("mybk","urn:samples")
expr.SetContext(mngr)
' select the nodes
Dim myXPathNodeIterator as XPathNodeIterator = nav.Select(expr)

 

Next I'll post the rest...

Software bugs are impossible to detect by anybody except the end user.
Posted

Another, and simple, way is to just query it like this:

   XmlDataDocument xmlDoc = new XmlDataDocument(); 
     
   XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("descendant::Customers[*/OrderDetails/ProductID=43]");

 

The SerlectNodes method also used XPath so just use the one you like the most...

 

This way you can pass this 'nodeList' object to where ever you want.

 

To save this nodes you've just queryed I think the only way is to create a new XMLDocument and Append the nodes iterating thru the nodeslist...

 

Alex :p

Software bugs are impossible to detect by anybody except the end user.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...