473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

LoadXml() - misunderstanding?

I think I might be misunderstanding just what the LoadXml method is
doing. I have 2 seemingly identical XmlDocuments, an XPath query
succeeds on one of them, and fails on the other. Can anyone tell me
what I'm doing wrong, and how to do it right?

[Test]
public void RetrieveDirectoryNumbersWithCirsAndDirs()
{
BillViewServiceRef.AccountRef acref = new
KWPrototype.WebServices.Tests.BillViewServiceRef.A ccountRef();
acref.AccountRefString = "2547049";

XmlNode dirNode;
XmlNode cirNode;

XmlDocument doc = BPBillView.RetrieveDirectoryNumbers(
AccountRef.Parse( "2547049" ) );

dirNode = doc.SelectSingleNode("//DirectoryNumbers");
cirNode = doc.SelectSingleNode("//CircuitNumbers");
Assert.IsNotNull(dirNode, "No DirectoryNumbers"); // This passes OK.
Assert.IsNotNull(dirNode, "No CircuitNumbers"); // This passes OK.

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml( doc.DocumentElement.OuterXml );

Assert.AreEqual( doc.DocumentElement.OuterXml,
doc2.DocumentElement.OuterXml ); // This passes OK.

dirNode = doc2.SelectSingleNode("//DirectoryNumbers");
cirNode = doc2.SelectSingleNode("//CircuitNumbers");
Assert.IsNotNull(dirNode, "No DirectoryNumbers"); // This fails.
Assert.IsNotNull(cirNode, "No CircuitNumbers"); // This fails.
}
Thanks,

Tim Haughton

Nov 12 '05 #1
4 3469
Tim Haughton wrote:
I think I might be misunderstanding just what the LoadXml method is
doing. I have 2 seemingly identical XmlDocuments, an XPath query
succeeds on one of them, and fails on the other. Can anyone tell me
what I'm doing wrong, and how to do it right?

[Test]
public void RetrieveDirectoryNumbersWithCirsAndDirs()
{
BillViewServiceRef.AccountRef acref = new
KWPrototype.WebServices.Tests.BillViewServiceRef.A ccountRef();
acref.AccountRefString = "2547049";

XmlNode dirNode;
XmlNode cirNode;

XmlDocument doc = BPBillView.RetrieveDirectoryNumbers(
AccountRef.Parse( "2547049" ) );

dirNode = doc.SelectSingleNode("//DirectoryNumbers");
cirNode = doc.SelectSingleNode("//CircuitNumbers");
Assert.IsNotNull(dirNode, "No DirectoryNumbers"); // This passes OK.
Assert.IsNotNull(dirNode, "No CircuitNumbers"); // This passes OK.

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml( doc.DocumentElement.OuterXml );

Assert.AreEqual( doc.DocumentElement.OuterXml,
doc2.DocumentElement.OuterXml ); // This passes OK.

dirNode = doc2.SelectSingleNode("//DirectoryNumbers");
cirNode = doc2.SelectSingleNode("//CircuitNumbers");
Assert.IsNotNull(dirNode, "No DirectoryNumbers"); // This fails.
Assert.IsNotNull(cirNode, "No CircuitNumbers"); // This fails.
}


Weird. Chances are namespaces are involved. E.g. when first document is
loaded and then some xmlns attributes are added, they won't affect XPath
untill XML is reloaded. Show ud how you build first XML.
Btw, instead of serializing and reparsing XML you can import nodes from
first XmlDocument into the second one - I believe that's more effective.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
Hi Oleg. The test had been altered a bit to emphasize the problem.
Essentially, we have some server side classes that constructs
XmlDocuments, these then get streamed across a web service, and I'm
basically checking that the XmlDocument that is constructed on the
client side passes the same tests as on the server side.

I wrote the documents out to disk to check was was different. The only
thing I could see was that the second document didn't have an
XmlDeclaration, so I added one. The test now looks like this...
[Test]
public void RetrieveDirectoryNumbersWithCirsAndDirs()
{
BillViewServiceRef.AccountRef acref = new
KWPrototype.WebServices.Tests.BillViewServiceRef.A ccountRef();
acref.AccountRefString = "2547049";

XmlNode dirNode;
XmlNode cirNode;

XmlDocument doc = BPBillView.RetrieveDirectoryNumbers(
AccountRef.Parse( "2547049" ) );
doc.Save( "C:\\Test.xml" );

dirNode = doc.SelectSingleNode("//DirectoryNumbers");
cirNode = doc.SelectSingleNode("//CircuitNumbers");
Assert.IsNotNull(dirNode, "No DirectoryNumbers"); // This passes OK.
Assert.IsNotNull(dirNode, "No CircuitNumbers"); // This passes OK.

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml( doc.DocumentElement.OuterXml );
doc2.PrependChild( doc2.CreateXmlDeclaration( "1.0", "utf-8", null ) );
doc2.Save( "c:\\Test2.xml" );

Assert.AreEqual( doc.DocumentElement.OuterXml,
doc2.DocumentElement.OuterXml ); // This passes OK.

dirNode = doc2.SelectSingleNode("//DirectoryNumbers");
cirNode = doc2.SelectSingleNode("//CircuitNumbers");
Assert.IsNotNull(dirNode, "No DirectoryNumbers"); // This fails.
Assert.IsNotNull(cirNode, "No CircuitNumbers"); // This fails.
}
After running the test, I Diff'd the two files - they were identical! A
little worrying!
Here's how the documents are constructed on the server side...
public static XmlDocument RetrieveDirectoryNumbers( AccountRef
accountRef )
{
double TotalSum = 0;
TODirCirNumbers mTODirCirNumbrs =
DataAccess.DARDirectoryNumbers.RetrieveDirCirNumbe rs( accountRef );

XmlDocument m = new XmlDocument();

KWXmlDocument mXmlDoc = new KWXmlDocument();

mXmlDoc.LoadXml("<Account></Account>");

XmlDeclaration myDeclaration =
mXmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
mXmlDoc.InsertBefore(myDeclaration, mXmlDoc.DocumentElement);

XmlAttribute mXmlNSAttr = mXmlDoc.CreateAttribute("xmlns");
mXmlNSAttr.Value = @"http://KCPrototype.com";
mXmlDoc.DocumentElement.Attributes.Append(mXmlNSAt tr);

XmlAttribute mAccRefAttr = mXmlDoc.CreateAttribute("AccountRef");
mAccRefAttr.Value = accountRef.ToString();
mXmlDoc.DocumentElement.Attributes.Append(mAccRefA ttr);

XmlAttribute mSpendAttr = mXmlDoc.CreateAttribute("SpendToDate");
mXmlDoc.DocumentElement.Attributes.Append(mSpendAt tr);

XmlDocument mTOXmlDoc = new XmlDocument();
mTOXmlDoc.LoadXml(mTODirCirNumbrs.toXml());

XmlNodeList mDirNmbrsNodes =
mTOXmlDoc.SelectNodes("//DirectoryNumbers");

double netcalls = 0;
double pNetcalls = 0;

foreach (XmlNode node in mDirNmbrsNodes)
{

XmlNode mAreaCodeNmbrNode = node.SelectSingleNode("//area_code");
XmlNode mSubNoNmbrNode = node.SelectSingleNode("//subscriber_no");
DirectoryNumber mDir =
DirectoryNumber.Parse(mAreaCodeNmbrNode.InnerText +
mSubNoNmbrNode.InnerText);

XmlNode mNode =
mXmlDoc.SelectSingleNode("//DirectoryNumbers[area_code='" +
mAreaCodeNmbrNode.InnerText + "' and subscriber_no='" +
mSubNoNmbrNode.InnerText + "']");
if(mNode != null)
{
XmlNode netCallsNode =
mXmlDoc.SelectSingleNode("//DirectoryNumbers[area_code='" +
mAreaCodeNmbrNode.InnerText + "' and subscriber_no='" +
mSubNoNmbrNode.InnerText + "']/net_calls");
pNetcalls = Double.Parse(netCallsNode.InnerText);
netcalls =
Double.Parse(node.SelectSingleNode("//DirectoryNumbers/net_calls").InnerText);
pNetcalls = pNetcalls + netcalls;
netCallsNode.InnerText = pNetcalls.ToString( "e3" );
TotalSum = TotalSum + netcalls;
}
else
{
XmlNode ImpNode = mXmlDoc.ImportNode(node, true);
mXmlDoc.DocumentElement.AppendChild(ImpNode);
}
}

//TODO: Eliminate loop for direct insert

XmlNodeList mCirNmbrsNodes = mTOXmlDoc.SelectNodes("//CircuitNumbers");
foreach (XmlNode node in mCirNmbrsNodes)
{
XmlNode ImpNode = mXmlDoc.ImportNode(node, true);
mXmlDoc.DocumentElement.AppendChild(ImpNode);
}

mSpendAttr.InnerText = TotalSum.ToString();

Assembly dtAssm = Assembly.GetAssembly( typeof( AccountRef ) );
StreamReader reader = new StreamReader(
dtAssm.GetManifestResourceStream(
"KWPrototype.DataTypes.Schema.RetrieveDirectoryNum bers.xsd" ) );
string mSchema = reader.ReadToEnd();

KWXmlDocument.ValidationResult result = mXmlDoc.Validate(mSchema);

if ( ! result.IsValid )
{
throw new XmlException( result.ToString() );
}

return mXmlDoc;
}
Probably doesn't make a whole load of sense. Essentially, we have a
data access layer that does some queries, the results are wrapped in
sanitised transfer objects, these transfer objects are then used to
construct the XmlDocument.
Even after all this, if the underlying XML is the same, of at least if
the output of the Save method is the same, then they should behave the
same.

Many thanks,

Tim Haughton

Nov 12 '05 #3
Oh, and this is the XmlDoc, in case you were wondering. Identical
output for the Save method on each document.

<?xml version="1.0" encoding="utf-8" ?>
- <Account xmlns="http://KCPrototype.com" AccountRef="2547049"
SpendToDate="176.928500772949">
- <DirectoryNumbers>
<client_id>0</client_id>
<area_code>01482</area_code>
<subscriber_id>589644</subscriber_id>
<subscriber_no>589644</subscriber_no>
<pbx_id>0</pbx_id>
<designation>B</designation>
<line_usage>VOC</line_usage>
<net_calls>1.914e+002</net_calls>
<contract_id>15752</contract_id>
<line_id>3</line_id>
<summary_ref>102122</summary_ref>
</DirectoryNumbers>
- <CircuitNumbers>
<client_id>0</client_id>
<circuit_no>MXHU10430</circuit_no>
<circuit_line_no>1</circuit_line_no>
<designation>B</designation>
<line_usage>PW</line_usage>
<net_calls>0</net_calls>
<contract_id>15752</contract_id>
<line_id>9</line_id>
<summary_ref>102122</summary_ref>
</CircuitNumbers>
- <CircuitNumbers>
<client_id>0</client_id>
<circuit_no>0800716544</circuit_no>
<circuit_line_no>1</circuit_line_no>
<designation>B</designation>
<line_usage>PW</line_usage>
<net_calls>0</net_calls>
<contract_id>15752</contract_id>
<line_id>10</line_id>
<summary_ref>102122</summary_ref>
</CircuitNumbers>
</Account>

Nov 12 '05 #4
Tim Haugton wrote:
Oh, and this is the XmlDoc, in case you were wondering. Identical
output for the Save method on each document.

<?xml version="1.0" encoding="utf-8" ?>
- <Account xmlns="http://KCPrototype.com" AccountRef="2547049"
SpendToDate="176.928500772949">
- <DirectoryNumbers>


Now I'm sure there is something wrong with you first document.
//DirectoryNumbers XPath can't select above element as in this XML
DirectoryNumbers belongs to "http://KCPrototype.com" namespace, while
//DirectoryNumbers selects DirectoryNumbers elements in no namespace.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: AFN | last post by:
I'm comfortable with VB.NET but new to XML. I am getting XML data from a remote machine across the internet. Sometimes my LoadXML call works fine and I parse out the data I need thereafter. ...
6
by: jmgonet | last post by:
Hello everybody, I'm having troubles loading a Xml string encoded in UTF-8. If I try this code: ------------------------------ XmlDocument doc=new XmlDocument(); String s="<?xml...
4
by: cloudx | last post by:
hi there, in VB LoadXML(str) returns true or false so that you can do different coding when the str is XML or not, but in C# LoadXML doesn't have return but only throw exception if str is not XML...
0
by: Reshma Prabhu | last post by:
Hello, I am using XmlDataDocument's LoadXml( ) function to load a particular xml string. This xml string contains a reference to a particular DTD. Though LoadXml function does not perform DTD or...
2
by: Lupina | last post by:
Hi I want load whole xml file, I try do it in the same way as I did it in windows application. try { XmlDocument myDoc = new XmlDocument();
2
by: binder | last post by:
How do I eliminate an extra backslash that is appearing after LoadXML call? This issue is causing an error with Process.Start. I have a string stored in sql: c:\program files\internet...
1
by: sumanmshan | last post by:
Hi everyone, This is my first post to this forum, hope I would get a reply quickly :-) Iam using loadXML(xmlString) and it is always returning "false". My code looks like this : if...
1
by: Beamor | last post by:
function art_menu_xml_parcer($content, $showSubMenus) { $doc = new DOMDocument(); $doc->loadXML($content);//this is the line in question $parent = $doc->documentElement; $elements =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.