HighTechTalks DotNet Forums  

XPath Call Quit Working

Dotnet XML microsoft.public.dotnet.xml


Discuss XPath Call Quit Working in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Bob
 
Posts: n/a

Default XPath Call Quit Working - 04-02-2004 , 04:26 PM






Below is some code that use to work. Now it is not and I have yet to figure out why. I want to select the nodes to a nodelist and remove them from the dom. The only thing I can figure is it broke when I added my schema.

XML

<?xml version="1.0" encoding="utf-8"?><IADSNOTES xmlns="notes.xsd" count="4"><Note href="D:\V4.0\DEMO\IADS_4_0.sgm#IADS V4.0 TOC@0" csm="0"><User>Ralph</User><Title>IADS 4.0 Demo - Table of Contents</Title><Text>Ralph's good note</Text></Note><Note href="D:\V4.0\DEMO\IADS_4_0.sgm#IADS V4.0 TOC@0" csm="0"><User>Ralph</User><Title>IADS 4.0 Demo - Table of Contents</Title><Text>RAlph's duplicate user</Text></Note><Note href="D:\V4.0\DEMO\IADS_4_0.sgm#IADS V4.0 TOC@0" csm="0"><User>RALPH</User><Title>IADS 4.0 Demo - Table of Contents</Title><Text>RALPH duplicate another way</Text></Note><Note href="D:\IADS\DEMO\IADS_4_0.sgm#GENENTITIES@0" csm="0"><User>Joe</User><Title>GENENTITIES</Title><Text>Frame 2 Public Note</Text></Note></IADSNOTES>

I am wanting to delete "joe" in a test.


Here is the C# code.


XmlNode parentNode;
XmlNodeList nodeList;

XmlDataDocument myXmlDataDocument = new XmlDataDocument();
XmlElement root = myXmlDataDocument.DocumentElement;

// lets see if a note file exits
string strNoteFile = string.Concat(strIadsPath, "\\Notes\\Public.udn");
FileInfo FI = new FileInfo(strNoteFile);
if(!FI.Exists)
return true;

FileStream FS = new FileStream(strNoteFile, FileMode.Open, FileAccess.Read, FileShare.Read);

StreamReader myStreamReader = new StreamReader(FS);
try
{
myXmlDataDocument.Load(myStreamReader);
}
catch(Exception ioe)
{
MessageBox.Show(ioe.Message.ToString(), "IADS Admin", MessageBoxButtons.OK,MessageBoxIcon.Error);
return false;
}
// set the root element

root = myXmlDataDocument.DocumentElement;

// since loop i/o is cheap we will pass through again
foreach (DataRow myDataRow in UserDeleteTbl.Rows)
{
if(myDataRow["Name"].ToString().IndexOf("Error") == -1)
{
// begin removing data
strFind = string.Concat("'",myDataRow["Name"].ToString(),"'");
strTemp = string.Concat("/IADSNOTES/Note[User = ",strFind,"]");
nodeList = root.SelectNodes(strTemp); // Node list of everything entered
for(i = 0; i < nodeList.Count; i++)
{
parentNode = nodeList.Item(i).ParentNode;
if(parentNode != null) //Make sure parent is good
{
if(parentNode.HasChildNodes) // lets check for sure
{
try
{
parentNode.RemoveChild(nodeList.Item(i)); // remove Note
iNotesDeleted++; // count how many we delete
}
catch(Exception er)
{
MessageBox.Show(er.Message.ToString());
return false;
}
}
}
} // end of looping through items and removing them.

}
}



Reply With Quote
  #2  
Old   
Derek Harmon
 
Posts: n/a

Default Re: XPath Call Quit Working - 04-02-2004 , 08:06 PM






"Bob" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
Below is some code that use to work. Now it is not
: :
The only thing I can figure is it broke when I added my schema.
: :
?xml version="1.0" encoding="utf-8"?><IADSNOTES xmlns="notes.xsd" count="4"
Technically, you haven't added a schema, only declared a default namespace.
As you've discovered, this can break the XPath expression if the instance
document had previously not used a namespace.

: :
Quote:
User>Joe</User><Title>GENENTITIES</Title><Text>Frame 2 Public
Note</Text></Note></IADSNOTES

I am wanting to delete "joe" in a test.
While evidently not the problem, remember that XPath expressions
are case-sensitive. "Joe" exists in the instance document, but "joe"
does not.

: :
Quote:
strFind = string.Concat("'",myDataRow["Name"].ToString(),"'");
strTemp = string.Concat("/IADSNOTES/Note[User = ",strFind,"]");
Immediately before these two lines of code, you must declare an
XmlNamespaceManager and associate a prefix, for instance, "ns".

XmlNamespaceManager xmlNs = new XmlNamespaceManager(
myXmlDataDocument.NameTable);
xmlNs.AddNamespace( "ns", "notes.xsd");

Then use this prefix in the XPath expression,

strFind = string.Concat("'",myDataRow["Name"].ToString(),"'");
strTemp = string.Concat("/ns:IADSNOTES/ns:Note[ns:User = ",strFind,"]");

It's necessary to use a prefix in the XPath expression, because an
empty string cannot be used as a prefix. As long as the underlying
namespace URI matches, what prefix is chosen for the XPath
Expression doesn't matter.


Derek Harmon




Reply With Quote
  #3  
Old   
Bob
 
Posts: n/a

Default Re: XPath Call Quit Working - 04-05-2004 , 09:11 AM



I am sorry I simply mistyped "joe". I tried what you mentioned with the namespace manager and now I am a little further. It appears to be throwing an exception.

"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."

The only place I can find an explination for this is on a newsgroup that only responds in german and I wish I could read the german.

Here is what I have now

// set the name space manager
XmlNamespaceManager xmlns = new XmlNamespaceManager(myXmlDataDocument.NameTable);
xmlns.AddNamespace("ns", "notes.xsd");

// set the root element
root = myXmlDataDocument.DocumentElement;


// since loop i/o is cheap we will pass through again
foreach (DataRow myDataRow in UserDeleteTbl.Rows)
{
if(myDataRow["Name"].ToString().IndexOf("Error") == -1)
{
// begin removing data
strFind = string.Concat("'",myDataRow["Name"].ToString(),"'");
strTemp = string.Concat("/ns:IADSNOTES/ns:Note[ns:User = ",strFind,"]");
try
{
nodeList = root.SelectNodes(strTemp); // Node list of everything entered
for(i = 0; i < nodeList.Count; i++)
{
parentNode = nodeList.Item(i).ParentNode;
if(parentNode != null) //Make sure parent is good
{
if(parentNode.HasChildNodes) // lets check for sure
{
try
{
parentNode.RemoveChild(nodeList.Item(i)); // remove Note
iNotesDeleted++; // count how many we delete
}
catch(Exception er)
{
MessageBox.Show(er.Message.ToString());
return false;
}
}
}
} // end of looping through items and removing them.
}
catch(Exception iob)
{
iob.Message.ToString();
}

}




Reply With Quote
  #4  
Old   
Derek Harmon
 
Posts: n/a

Default Re: XPath Call Quit Working - 04-06-2004 , 09:32 PM



"Bob" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
"Namespace Manager or XsltContext needed. This query has a prefix,
variable, or user-defined function."
: :
XmlNamespaceManager xmlns = new XmlNamespaceManager
(myXmlDataDocument.NameTable);
xmlns.AddNamespace("ns", "notes.xsd");
: :
nodeList = root.SelectNodes(strTemp); // Node list of everything entered
Use the Namespace Manager,

nodeList = root.SelectNodes( strTemp, xmlns);


Derek Harmon




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.