473,734 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XPathNavigator SetValue wipes out XmlType

Hi.

Using VS2005, .NET 2.0.

I have an xml document that I want to go through and set the values on
attributes of elements. The elements are complex types defined in my schema
(xsd) files.

I can iterate the document and get my XmlType and XmlBaseType values just
fine. However, as soon as I call SetValue to write to an attribute, the
XmlType is always null so I can no longer test the rest of the elements in
the document.

Following are the Xsd, Xml and Program.cs to recreate the problem.

-- TestXPathNavBug SchemaBase.xsd --
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mine="htt p://mytest.ca/mine" targetNamespace ="http://mytest.ca/mine"
elementFormDefa ult="qualified" attributeFormDe fault="unqualif ied">
<xs:complexTy pe name="OrderBase ">
<xs:annotatio n>
<xs:documentati on>Base Schema for Orders</xs:documentatio n>
</xs:annotation>
<xs:attribute name="Name" type="xs:string " use="required"/>
<xs:attribute name="Updatable " type="xs:boolea n" use="required"
fixed="true"/>
</xs:complexType>
<xs:complexTy pe name="PurchaseO rderBase">
<xs:complexCont ent>
<xs:extension base="mine:Orde rBase">
<xs:attribute name="PurchaseO rderNumber" type="xs:string " use="required"/>
</xs:extension>
</xs:complexConte nt>
</xs:complexType>
<xs:complexTy pe name="OrderItem Base">
<xs:annotatio n>
<xs:documentati on>Base Schema for Order items</xs:documentatio n>
</xs:annotation>
<xs:attribute name="ItemType" type="xs:string " use="required"/>
<xs:attribute name="ItemDescr iption" type="xs:string " use="required"/>
</xs:complexType>
<xs:complexTy pe name="PurchaseO rderItemBase">
<xs:complexCont ent>
<xs:extension base="mine:Orde rItemBase">
<xs:attribute name="PurchaseO rderNumber" type="xs:string " use="required"/>
</xs:extension>
</xs:complexConte nt>
</xs:complexType>
</xs:schema>
-- TestXPathNavBug Schema.xsd --
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mine="htt p://mytest.ca/mine" targetNamespace ="http://mytest.ca/mine"
elementFormDefa ult="qualified" attributeFormDe fault="unqualif ied">
<xs:include schemaLocation= "TestXPathNavBu gSchemaBase.xsd "/>
<xs:complexTy pe name="AutoParts PurchaseOrderTy pe">
<xs:complexCont ent>
<xs:extension base="mine:Purc haseOrderBase">
<xs:sequence>
<xs:element name="Header" type="mine:Purc haseOrderItemBa se"/>
<xs:element name="Body" type="mine:Purc haseOrderItemBa se"/>
<xs:element name="Footer" type="mine:Purc haseOrderItemBa se"/>
</xs:sequence>
</xs:extension>
</xs:complexConte nt>
</xs:complexType>
<xs:element name="AutoParts PurchaseContrac t">
<xs:complexType >
<xs:sequence>
<xs:element name="PurchaseO rders" type="mine:Auto PartsPurchaseOr derType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

-- TestXPathNavBug .xml --
<?xml version="1.0" encoding="UTF-8"?>
<AutoPartsPurch aseContract xmlns="http://mytest.ca/mine"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocat ion="http://mytest.ca/mine TestXPathNavBug Schema.xsd">
<PurchaseOrde rs Updatable="true " Name="" PurchaseOrderNu mber="">
<Header ItemType="" ItemDescription ="" PurchaseOrderNu mber="" />
<Body ItemType="" ItemDescription ="" PurchaseOrderNu mber="" />
<Footer ItemType="" ItemDescription ="" PurchaseOrderNu mber="" />
</PurchaseOrders>
</AutoPartsPurcha seContract>

-- Program.cs --
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPat h;
using System.Xml.Sche ma;

namespace TestXPathNavBug
{
class Program
{
static void Main(string[] args)
{
const string xmlFileName = @"..\..\TestXPa thNavBug.xml";

//Load the xml with its xsd
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Schema s.Add("http://mytest.ca/mine",
@"..\..\TestXPa thNavBugSchema. xsd");
settings.Valida tionType = ValidationType. Schema;

XmlReader reader = XmlReader.Creat e(xmlFileName, settings);

XmlDocument doc = new XmlDocument();
doc.Load(reader );

//Create an XPathNavigator
XPathNavigator xpath = doc.CreateNavig ator();

// Start at the top
xpath.MoveToRoo t();
//Modify the xml document
TraverseTree(xp ath);

//Save
reader.Close();
doc.Save(xmlFil eName);

Console.ReadKey ();
}

private static void TraverseTree(XP athNavigator xpath)
{
//Process this node
if (xpath.NodeType == XPathNodeType.E lement)
{
Console.WriteLi ne("Element: {0}", xpath.Name);

//See if this is one of our schema types
XmlSchemaType xmlBaseType = null;
XmlSchemaType xmlType = xpath.XmlType;
if (xmlType != null)
{
if (!string.IsNull OrEmpty(xmlType .Name))
Console.WriteLi ne(" XmlType: {0}", xmlType.Name);

xmlBaseType = xmlType.BaseXml SchemaType;
if (!string.IsNull OrEmpty(xmlBase Type.Name))
Console.WriteLi ne(" XmlBaseType: {0}",
xmlBaseType.Nam e);
}

//These are the attributes we want to populate in the XML
file.
if (xmlBaseType != null
&& !string.IsNullO rEmpty(xmlBaseT ype.Name)
&& xmlBaseType.Nam e.Equals("Order ItemBase",
StringCompariso n.OrdinalIgnore Case))
{
if (xpath.HasAttri butes)
{
//Grab a clone to start navigating at this position
XPathNavigator navAttributes = xpath.Clone();

navAttributes.M oveToFirstAttri bute();

do
{
Console.WriteLi ne(" Attribute: {0}, can edit?
{1}", navAttributes.N ame, navAttributes.C anEdit);

string valueToSave = string.Empty;
switch (navAttributes. Name.ToLowerInv ariant())
{
case "itemtype":
valueToSave = string.Format(" {0}{1}",
navAttributes.N ame, xmlType.Name);
break;

case "itemdescriptio n":
valueToSave = string.Format(" Describe
{0}{1}", navAttributes.N ame, xmlType.Name);
break;

case "purchaseordern umber":
valueToSave =
DateTime.Now.Ti cks.ToString();
break;
}

//TODO: This reproduces the bug.
/* If you call the SetValue, then the XmlTypes
are wiped out on
* subsequent Moves.
* */
if (!string.IsNull OrEmpty(valueTo Save))
navAttributes.S etValue(valueTo Save);

} while (navAttributes. MoveToNextAttri bute());

}
}
}

//Go down the branch
if (xpath.HasChild ren)
{
xpath.MoveToFir stChild();

do
{
TraverseTree(xp ath);
} while (xpath.MoveToNe xt());

//Go back to the top of the branch
xpath.MoveToPar ent();
}
}
}
}

May 23 '07 #1
2 4539
Hi Noremac,

Please see following document:

#Modify XML Data using XPathNavigator
http://msdn2.microsoft.com/en-us/lib...x1(VS.80).aspx
Specifically, if the child elements or attributes of an element are
inserted, updated, or deleted, then the validity of the element becomes
unknown. This is represented by the Validity property of the element's
SchemaInfo property being set to NotKnown. Furthermore, this effect
cascades upwards recursively across the XML document, because the validity
of the element's parent element (and its parent element, and so on) also
becomes unknown.

When you changed the first element's attributes, its SchemaInfo.Vali dity
becomes NotKnown; and the XmlType will need Validity with Valid value to
return the correct type.

To fix this, you need to call XmlDocument.Val idate again after you changed
the element's attributes.
Hope this helps.
Sincerely,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

May 24 '07 #2
Sweet! I was crossing my fingers it was something simple like that!

"Walter Wang [MSFT]" wrote:
Hi Noremac,

Please see following document:

#Modify XML Data using XPathNavigator
http://msdn2.microsoft.com/en-us/lib...x1(VS.80).aspx
Specifically, if the child elements or attributes of an element are
inserted, updated, or deleted, then the validity of the element becomes
unknown. This is represented by the Validity property of the element's
SchemaInfo property being set to NotKnown. Furthermore, this effect
cascades upwards recursively across the XML document, because the validity
of the element's parent element (and its parent element, and so on) also
becomes unknown.

When you changed the first element's attributes, its SchemaInfo.Vali dity
becomes NotKnown; and the XmlType will need Validity with Valid value to
return the correct type.

To fix this, you need to call XmlDocument.Val idate again after you changed
the element's attributes.
Hope this helps.
Sincerely,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

May 24 '07 #3

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

Similar topics

1
14422
by: rabbit | last post by:
Hi all, I want to know, how can i insert the xml data using createXML() with CLOB in the xmltype column? I store the xml data at first in the clob column und want to import these data in the xmltype column in another table. But i get always the error message: java.sql.SQLException: ORA-00600: internal error code, arguments: , , , , , , , ORA-06512: in "SYS.XMLTYPE", line 0
1
3377
by: Craig Pearson | last post by:
Hi My function receives an XPathNavigator object. From here I need to build a DataSet to load into SQL Server (using SQLXML adaptor). Does anyone have an idea on the most efficient way to convert the XPathNavigator into a DataSet? Here is what I have at the moment: void function DoSomething( string xmlSchemeFile, System.Xml.XPath.XPathNavigator nav ) {
4
2782
by: Bradley Plett | last post by:
I have a relatively simple xsd which I am turning into a class using "xsd.exe". I then create a collection of these classes. I have run into one minor problem. When serializing my collection, I would like the names of the elements to be different from the class name as generated by xsd.exe. I don't want to change the class name itself, due to naming conventions. This is trivial to do after-the-fact by adding the "XmlType" tag to the...
7
4087
by: David Thielen | last post by:
Hi; Is there a way from an XPathNavigator object to get an xpath string that will, when used in a Select(xpath) on the underlying base/root XPathNavigator return the same XPathNavigator? In other words, I initially create an XPathNavigator for my entire xml document. To get an XPathNavigator object who's root is a given node in the original xml document, there is a unique xpath that will return that node. The unique xpath could have,...
1
3614
by: jon cosby | last post by:
Not sure why this doesn't work. The node values are unchanged. Is there something that needs to be done to accept the changes? sXmlPath = Application.StartupPath.ToString() + \\settings.xml"; XmlDocument xmlConfig = new XmlDocument(); xmlConfig.Load(sXmlPath); XPathNavigator navigator = xmlConfig.CreateNavigator();
11
6464
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString); XPathNavigator nav = docNav.CreateNavigator(); XmlDocument doc = new XmlDocument(); doc.LoadXml("<DocumentData></DocumentData>"); XmlElement elem = doc.CreateElement(currentNodeName);
0
1793
by: kelvin273 | last post by:
Hi all, i'm new in this community (and in .NET programming) and i've a problem with xpathnavigator. The idea is to have an xml document with attribute editable by windows form. I write this code, where xPath is, of course, the correct path of the selected node, and NodeEntity is a class that encapsulate the new attribute value. The method extract value from NodeEntity and update attribute value of the current node. But i don't find the...
0
2639
by: stepby | last post by:
Hi All, I am using the ASP as the server side language. I would like to ask how to retrieve the whole xml form the xmltype datatype in the database. I have found some SQL example to retrieve specific information in the xmltype datatype. SELECT a.col1.extract('//TABLE_NAME').getStringVal() as hello FROM tab1 a And I would like to ask how to retrieve the whole xml and store in what kinds of object in ASP.
0
2129
by: =?Utf-8?B?bW90eWxpaw==?= | last post by:
I want to expose my classes via web services in a different format. Using XmlAttributeAttribute and XmlElementAttribute works fine. But XmlType does not in all cases. If I have a class decl: public class InternalFoo {} the schema generated is just fine for public internalFoo. But if I have a
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
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
9449
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
9310
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...
1
9236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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...
1
3261
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
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.