473,796 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LoadXml() - misunderstandin g?

I think I might be misunderstandin g 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 RetrieveDirecto ryNumbersWithCi rsAndDirs()
{
BillViewService Ref.AccountRef acref = new
KWPrototype.Web Services.Tests. BillViewService Ref.AccountRef( );
acref.AccountRe fString = "2547049";

XmlNode dirNode;
XmlNode cirNode;

XmlDocument doc = BPBillView.Retr ieveDirectoryNu mbers(
AccountRef.Pars e( "2547049" ) );

dirNode = doc.SelectSingl eNode("//DirectoryNumber s");
cirNode = doc.SelectSingl eNode("//CircuitNumbers" );
Assert.IsNotNul l(dirNode, "No DirectoryNumber s"); // This passes OK.
Assert.IsNotNul l(dirNode, "No CircuitNumbers" ); // This passes OK.

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml( doc.DocumentEle ment.OuterXml );

Assert.AreEqual ( doc.DocumentEle ment.OuterXml,
doc2.DocumentEl ement.OuterXml ); // This passes OK.

dirNode = doc2.SelectSing leNode("//DirectoryNumber s");
cirNode = doc2.SelectSing leNode("//CircuitNumbers" );
Assert.IsNotNul l(dirNode, "No DirectoryNumber s"); // This fails.
Assert.IsNotNul l(cirNode, "No CircuitNumbers" ); // This fails.
}
Thanks,

Tim Haughton

Nov 12 '05 #1
4 3502
Tim Haughton wrote:
I think I might be misunderstandin g 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 RetrieveDirecto ryNumbersWithCi rsAndDirs()
{
BillViewService Ref.AccountRef acref = new
KWPrototype.Web Services.Tests. BillViewService Ref.AccountRef( );
acref.AccountRe fString = "2547049";

XmlNode dirNode;
XmlNode cirNode;

XmlDocument doc = BPBillView.Retr ieveDirectoryNu mbers(
AccountRef.Pars e( "2547049" ) );

dirNode = doc.SelectSingl eNode("//DirectoryNumber s");
cirNode = doc.SelectSingl eNode("//CircuitNumbers" );
Assert.IsNotNul l(dirNode, "No DirectoryNumber s"); // This passes OK.
Assert.IsNotNul l(dirNode, "No CircuitNumbers" ); // This passes OK.

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml( doc.DocumentEle ment.OuterXml );

Assert.AreEqual ( doc.DocumentEle ment.OuterXml,
doc2.DocumentEl ement.OuterXml ); // This passes OK.

dirNode = doc2.SelectSing leNode("//DirectoryNumber s");
cirNode = doc2.SelectSing leNode("//CircuitNumbers" );
Assert.IsNotNul l(dirNode, "No DirectoryNumber s"); // This fails.
Assert.IsNotNul l(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 RetrieveDirecto ryNumbersWithCi rsAndDirs()
{
BillViewService Ref.AccountRef acref = new
KWPrototype.Web Services.Tests. BillViewService Ref.AccountRef( );
acref.AccountRe fString = "2547049";

XmlNode dirNode;
XmlNode cirNode;

XmlDocument doc = BPBillView.Retr ieveDirectoryNu mbers(
AccountRef.Pars e( "2547049" ) );
doc.Save( "C:\\Test.x ml" );

dirNode = doc.SelectSingl eNode("//DirectoryNumber s");
cirNode = doc.SelectSingl eNode("//CircuitNumbers" );
Assert.IsNotNul l(dirNode, "No DirectoryNumber s"); // This passes OK.
Assert.IsNotNul l(dirNode, "No CircuitNumbers" ); // This passes OK.

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml( doc.DocumentEle ment.OuterXml );
doc2.PrependChi ld( doc2.CreateXmlD eclaration( "1.0", "utf-8", null ) );
doc2.Save( "c:\\Test2. xml" );

Assert.AreEqual ( doc.DocumentEle ment.OuterXml,
doc2.DocumentEl ement.OuterXml ); // This passes OK.

dirNode = doc2.SelectSing leNode("//DirectoryNumber s");
cirNode = doc2.SelectSing leNode("//CircuitNumbers" );
Assert.IsNotNul l(dirNode, "No DirectoryNumber s"); // This fails.
Assert.IsNotNul l(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 RetrieveDirecto ryNumbers( AccountRef
accountRef )
{
double TotalSum = 0;
TODirCirNumbers mTODirCirNumbrs =
DataAccess.DARD irectoryNumbers .RetrieveDirCir Numbers( accountRef );

XmlDocument m = new XmlDocument();

KWXmlDocument mXmlDoc = new KWXmlDocument() ;

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

XmlDeclaration myDeclaration =
mXmlDoc.CreateX mlDeclaration(" 1.0","utf-8",null);
mXmlDoc.InsertB efore(myDeclara tion, mXmlDoc.Documen tElement);

XmlAttribute mXmlNSAttr = mXmlDoc.CreateA ttribute("xmlns ");
mXmlNSAttr.Valu e = @"http://KCPrototype.com ";
mXmlDoc.Documen tElement.Attrib utes.Append(mXm lNSAttr);

XmlAttribute mAccRefAttr = mXmlDoc.CreateA ttribute("Accou ntRef");
mAccRefAttr.Val ue = accountRef.ToSt ring();
mXmlDoc.Documen tElement.Attrib utes.Append(mAc cRefAttr);

XmlAttribute mSpendAttr = mXmlDoc.CreateA ttribute("Spend ToDate");
mXmlDoc.Documen tElement.Attrib utes.Append(mSp endAttr);

XmlDocument mTOXmlDoc = new XmlDocument();
mTOXmlDoc.LoadX ml(mTODirCirNum brs.toXml());

XmlNodeList mDirNmbrsNodes =
mTOXmlDoc.Selec tNodes("//DirectoryNumber s");

double netcalls = 0;
double pNetcalls = 0;

foreach (XmlNode node in mDirNmbrsNodes)
{

XmlNode mAreaCodeNmbrNo de = node.SelectSing leNode("//area_code");
XmlNode mSubNoNmbrNode = node.SelectSing leNode("//subscriber_no") ;
DirectoryNumber mDir =
DirectoryNumber .Parse(mAreaCod eNmbrNode.Inner Text +
mSubNoNmbrNode. InnerText);

XmlNode mNode =
mXmlDoc.SelectS ingleNode("//DirectoryNumber s[area_code='" +
mAreaCodeNmbrNo de.InnerText + "' and subscriber_no=' " +
mSubNoNmbrNode. InnerText + "']");
if(mNode != null)
{
XmlNode netCallsNode =
mXmlDoc.SelectS ingleNode("//DirectoryNumber s[area_code='" +
mAreaCodeNmbrNo de.InnerText + "' and subscriber_no=' " +
mSubNoNmbrNode. InnerText + "']/net_calls");
pNetcalls = Double.Parse(ne tCallsNode.Inne rText);
netcalls =
Double.Parse(no de.SelectSingle Node("//DirectoryNumber s/net_calls").Inn erText);
pNetcalls = pNetcalls + netcalls;
netCallsNode.In nerText = pNetcalls.ToStr ing( "e3" );
TotalSum = TotalSum + netcalls;
}
else
{
XmlNode ImpNode = mXmlDoc.ImportN ode(node, true);
mXmlDoc.Documen tElement.Append Child(ImpNode);
}
}

//TODO: Eliminate loop for direct insert

XmlNodeList mCirNmbrsNodes = mTOXmlDoc.Selec tNodes("//CircuitNumbers" );
foreach (XmlNode node in mCirNmbrsNodes)
{
XmlNode ImpNode = mXmlDoc.ImportN ode(node, true);
mXmlDoc.Documen tElement.Append Child(ImpNode);
}

mSpendAttr.Inne rText = TotalSum.ToStri ng();

Assembly dtAssm = Assembly.GetAss embly( typeof( AccountRef ) );
StreamReader reader = new StreamReader(
dtAssm.GetManif estResourceStre am(
"KWPrototype.Da taTypes.Schema. RetrieveDirecto ryNumbers.xsd" ) );
string mSchema = reader.ReadToEn d();

KWXmlDocument.V alidationResult result = mXmlDoc.Validat e(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="254 7049"
SpendToDate="17 6.928500772949" >
- <DirectoryNumbe rs>
<client_id>0</client_id>
<area_code>0148 2</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.91 4e+002</net_calls>
<contract_id>15 752</contract_id>
<line_id>3</line_id>
<summary_ref>10 2122</summary_ref>
</DirectoryNumber s>
- <CircuitNumbers >
<client_id>0</client_id>
<circuit_no>MXH U10430</circuit_no>
<circuit_line_n o>1</circuit_line_no >
<designation> B</designation>
<line_usage>P W</line_usage>
<net_calls>0</net_calls>
<contract_id>15 752</contract_id>
<line_id>9</line_id>
<summary_ref>10 2122</summary_ref>
</CircuitNumbers>
- <CircuitNumbers >
<client_id>0</client_id>
<circuit_no>080 0716544</circuit_no>
<circuit_line_n o>1</circuit_line_no >
<designation> B</designation>
<line_usage>P W</line_usage>
<net_calls>0</net_calls>
<contract_id>15 752</contract_id>
<line_id>10</line_id>
<summary_ref>10 2122</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="254 7049"
SpendToDate="17 6.928500772949" >
- <DirectoryNumbe rs>


Now I'm sure there is something wrong with you first document.
//DirectoryNumber s XPath can't select above element as in this XML
DirectoryNumber s belongs to "http://KCPrototype.com " namespace, while
//DirectoryNumber s selects DirectoryNumber s 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
6053
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. But other times, LoadXML fails because it says it can't find a DTD file which is referenced in <!DOCTYPE Response SYSTEM "The_File.DTD">. I don't know what a DTD file is, but I don't have it and I'm just trying to parse the rest of the XML. ...
6
18768
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 version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><a>Schönbühl</a>"; doc.LoadXml(s); doc.Save("d:\\temp\\test.xml");
4
17813
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 document. How would I do if I want to do my own code if str is not an XML? thanks!
0
559
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 schema validation, it tries to find that DTD. I want that LoadXml function should not find the DTD even though xml string has a reference to it.
2
2762
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
2873
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 explorer\iexplore.exe If I look at this data when I write the dataset to an XML file, the data is correct.
1
2023
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 (window.ActiveXObject) { var responseXml=new ActiveXObject("Microsoft.XMLDOM"); responseXml.async="false";
1
6474
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 = $parent->childNodes; need help. my site worked fine on my localhost but when i uploaded it to my live server i keep getting this error need help to recode line to match my hosting server. im using php 5 i have attached the common_method file
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.