Iterating XML with schema info?

G

Gina_Marano

Please help. I don't have much hair left to pull out after struggling
over this into the wee hours of the morning.

I am having a tough time iterating through the following XML. I need
to evaluate each <Queue..> entry.

<?xml version="1.0" encoding="UTF-8"?>
<JMF xmlns="http://www.CIP4.org/JDFSchema_1_1" Version="1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Response ID="RDFS1934507113653e9" ReturnCode="0" Type="QueueStatus"
refID="132413dj323s223" xsi:type="ResponseQueueStatus">
<Queue DeviceID="Dev01" QueueSize="8" Status="Waiting">
<QueueEntry DeviceID="vl-5001" JobID="1048655" JobPartID="79">
<Preview URL="http://192.168.1.2/preview?job=1048655"/>
<JobPhase Amount="2000" DescriptiveName="12.jpg"
PercentCompleted="0"/>
</QueueEntry>
<QueueEntry DeviceID="Dev02" JobID="1048651" JobPartID="75">
<Preview URL="http://192.168.1.2/preview?job=1043654"/>
<JobPhase Amount="2000" DescriptiveName="13.jpg"
PercentCompleted="0"/>
</QueueEntry>
</Queue>
</Response>
</JMF>

(hopefully you can copy and paste it to a pretty editor)

XPathDocument xpathDocument = new XPathDocument(sFileName);
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
XPathExpression xpathExpression = xpathNavigator.Compile("JMF/Response/
Queue/QueueEntry");

XPathNodeIterator xpathIterator =
xpathNavigator.Select(xpathExpression);
foreach (XPathNavigator node in xpathIterator)
{
string sID = node.GetAttribute("DeviceID", "");
...
}

Anything special I need to do since the XML has schema information in
it?

I also am not tied to XPathDocument but I need something efficient
because there can be hundreds of Queue "listings".

Any information would be extremely appreciated!

Good day y'all.

~Gina_M~
 
G

Guest

Hi Gina,

Try this piece of code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(doc);

XmlNamespaceManager nm = new
XmlNamespaceManager(xmlDoc.NameTable);
nm.AddNamespace("my", "http://www.CIP4.org/JDFSchema_1_1");

XmlNodeList list =
xmlDoc.SelectNodes("/my:JMF/my:Response/my:Queue/my:QueueEntry", nm);
foreach (XmlNode node in list)
{
string sID = node.Attributes["DeviceID"].Value;
}


How it works:

<JMF xmlns=""http://www.CIP4.org/JDFSchema_1_1"" ...> - this means that the
each node in the document is in the "http://www.CIP4.org/..." namespace.

So by trying to find a "/JMF" node returns zero nodes - because this implies
that the namespace of that node is "".

Therefore - you have to create a NamespaceManager, and add your own prefix
for the namespace, e.g. "my".

Then you have to use that prefix when querying for nodes:
("/my:JMF/my:Response/my:Queue/my:QueueEntry", nm);


Hope this helps,

_____________
Adam Bieganski
http://godevelop.blogspot.com
 
G

Gina_Marano

Ahhhhh, your my hero!

I found something close to this but I never thought I had to
put the namespace after everything in the expstring:

"//ns:JMF/ns:Response/ns:Queue/ns:QueueEntry"

I would have been at this forever!

Much thanks! and thanks for the explaination. I understand more now,
looks funny but makes sense.

~Gina_M~

Hi Gina,

Try this piece of code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(doc);

XmlNamespaceManager nm = new
XmlNamespaceManager(xmlDoc.NameTable);
nm.AddNamespace("my", "http://www.CIP4.org/JDFSchema_1_1");

XmlNodeList list =
xmlDoc.SelectNodes("/my:JMF/my:Response/my:Queue/my:QueueEntry", nm);
foreach (XmlNode node in list)
{
string sID = node.Attributes["DeviceID"].Value;
}

How it works:

<JMF xmlns=""http://www.CIP4.org/JDFSchema_1_1"" ...> - this means that the
each node in the document is in the "http://www.CIP4.org/..." namespace.

So by trying to find a "/JMF" node returns zero nodes - because this implies
that the namespace of that node is "".

Therefore - you have to create a NamespaceManager, and add your own prefix
for the namespace, e.g. "my".

Then you have to use that prefix when querying for nodes:
("/my:JMF/my:Response/my:Queue/my:QueueEntry", nm);

Hope this helps,

_____________
Adam Bieganskihttp://godevelop.blogspot.com



Gina_Marano said:
Please help. I don't have much hair left to pull out after struggling
over this into the wee hours of the morning.
I am having a tough time iterating through the following XML. I need
to evaluate each <Queue..> entry.
<?xml version="1.0" encoding="UTF-8"?>
<JMF xmlns="http://www.CIP4.org/JDFSchema_1_1" Version="1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Response ID="RDFS1934507113653e9" ReturnCode="0" Type="QueueStatus"
refID="132413dj323s223" xsi:type="ResponseQueueStatus">
<Queue DeviceID="Dev01" QueueSize="8" Status="Waiting">
<QueueEntry DeviceID="vl-5001" JobID="1048655" JobPartID="79">
<Preview URL="http://192.168.1.2/preview?job=1048655"/>
<JobPhase Amount="2000" DescriptiveName="12.jpg"
PercentCompleted="0"/>
</QueueEntry>
<QueueEntry DeviceID="Dev02" JobID="1048651" JobPartID="75">
<Preview URL="http://192.168.1.2/preview?job=1043654"/>
<JobPhase Amount="2000" DescriptiveName="13.jpg"
PercentCompleted="0"/>
</QueueEntry>
</Queue>
</Response>
</JMF>
(hopefully you can copy and paste it to a pretty editor)
XPathDocument xpathDocument = new XPathDocument(sFileName);
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
XPathExpression xpathExpression = xpathNavigator.Compile("JMF/Response/
Queue/QueueEntry");
XPathNodeIterator xpathIterator =
xpathNavigator.Select(xpathExpression);
foreach (XPathNavigator node in xpathIterator)
{
string sID = node.GetAttribute("DeviceID", "");
...
}
Anything special I need to do since the XML has schema information in
it?
I also am not tied to XPathDocument but I need something efficient
because there can be hundreds of Queue "listings".
Any information would be extremely appreciated!
Good day y'all.
~Gina_M~- Hide quoted text -

- Show quoted text -
 
G

Gina_Marano

Here is the working code for other poor souls:

XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(@"c:\my.xml");

//Create a string containing the XPATH expression to evaluate.
string sExpr = "//ns:JMF/ns:Response/ns:Queue/ns:QueueEntry";

//Create an XmlNamespaceManager and add the namespaces for the
document.
XmlNamespaceManager nsmanager = new
XmlNamespaceManager(myXmlDocument.NameTable);

//map namespaces to prefixes for querying purposes
nsmanager.AddNamespace("ns", "http://www.CIP4.org/JDFSchema_1_1");

XmlNodeList nodelist = myXmlDocument.SelectNodes(sExpr, nsmanager);

foreach (XmlNode myXmlNode in nodelist)
{
string sID = myXmlNode.Attributes["DeviceID"].Value; ;
...
}

~Gina_M~
 
G

Guest

Glad I could help! :)


_____________
Adam Bieganski
http://godevelop.blogspot.com


Gina_Marano said:
Ahhhhh, your my hero!

I found something close to this but I never thought I had to
put the namespace after everything in the expstring:

"//ns:JMF/ns:Response/ns:Queue/ns:QueueEntry"

I would have been at this forever!

Much thanks! and thanks for the explaination. I understand more now,
looks funny but makes sense.

~Gina_M~

Hi Gina,

Try this piece of code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(doc);

XmlNamespaceManager nm = new
XmlNamespaceManager(xmlDoc.NameTable);
nm.AddNamespace("my", "http://www.CIP4.org/JDFSchema_1_1");

XmlNodeList list =
xmlDoc.SelectNodes("/my:JMF/my:Response/my:Queue/my:QueueEntry", nm);
foreach (XmlNode node in list)
{
string sID = node.Attributes["DeviceID"].Value;
}

How it works:

<JMF xmlns=""http://www.CIP4.org/JDFSchema_1_1"" ...> - this means that the
each node in the document is in the "http://www.CIP4.org/..." namespace.

So by trying to find a "/JMF" node returns zero nodes - because this implies
that the namespace of that node is "".

Therefore - you have to create a NamespaceManager, and add your own prefix
for the namespace, e.g. "my".

Then you have to use that prefix when querying for nodes:
("/my:JMF/my:Response/my:Queue/my:QueueEntry", nm);

Hope this helps,

_____________
Adam Bieganskihttp://godevelop.blogspot.com



Gina_Marano said:
Please help. I don't have much hair left to pull out after struggling
over this into the wee hours of the morning.
I am having a tough time iterating through the following XML. I need
to evaluate each <Queue..> entry.
<?xml version="1.0" encoding="UTF-8"?>
<JMF xmlns="http://www.CIP4.org/JDFSchema_1_1" Version="1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Response ID="RDFS1934507113653e9" ReturnCode="0" Type="QueueStatus"
refID="132413dj323s223" xsi:type="ResponseQueueStatus">
<Queue DeviceID="Dev01" QueueSize="8" Status="Waiting">
<QueueEntry DeviceID="vl-5001" JobID="1048655" JobPartID="79">
<Preview URL="http://192.168.1.2/preview?job=1048655"/>
<JobPhase Amount="2000" DescriptiveName="12.jpg"
PercentCompleted="0"/>
</QueueEntry>
<QueueEntry DeviceID="Dev02" JobID="1048651" JobPartID="75">
<Preview URL="http://192.168.1.2/preview?job=1043654"/>
<JobPhase Amount="2000" DescriptiveName="13.jpg"
PercentCompleted="0"/>
</QueueEntry>
</Queue>
</Response>
</JMF>
(hopefully you can copy and paste it to a pretty editor)
XPathDocument xpathDocument = new XPathDocument(sFileName);
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
XPathExpression xpathExpression = xpathNavigator.Compile("JMF/Response/
Queue/QueueEntry");
XPathNodeIterator xpathIterator =
xpathNavigator.Select(xpathExpression);
foreach (XPathNavigator node in xpathIterator)
{
string sID = node.GetAttribute("DeviceID", "");
...
}
Anything special I need to do since the XML has schema information in
it?
I also am not tied to XPathDocument but I need something efficient
because there can be hundreds of Queue "listings".
Any information would be extremely appreciated!
Good day y'all.
~Gina_M~- Hide quoted text -

- Show quoted text -
 
Top