473,385 Members | 1,312 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,385 software developers and data experts.

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.

-- TestXPathNavBugSchemaBase.xsd --
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mine="http://mytest.ca/mine" targetNamespace="http://mytest.ca/mine"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="OrderBase">
<xs:annotation>
<xs:documentation>Base Schema for Orders</xs:documentation>
</xs:annotation>
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:attribute name="Updatable" type="xs:boolean" use="required"
fixed="true"/>
</xs:complexType>
<xs:complexType name="PurchaseOrderBase">
<xs:complexContent>
<xs:extension base="mine:OrderBase">
<xs:attribute name="PurchaseOrderNumber" type="xs:string" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="OrderItemBase">
<xs:annotation>
<xs:documentation>Base Schema for Order items</xs:documentation>
</xs:annotation>
<xs:attribute name="ItemType" type="xs:string" use="required"/>
<xs:attribute name="ItemDescription" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="PurchaseOrderItemBase">
<xs:complexContent>
<xs:extension base="mine:OrderItemBase">
<xs:attribute name="PurchaseOrderNumber" type="xs:string" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
-- TestXPathNavBugSchema.xsd --
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mine="http://mytest.ca/mine" targetNamespace="http://mytest.ca/mine"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="TestXPathNavBugSchemaBase.xsd"/>
<xs:complexType name="AutoPartsPurchaseOrderType">
<xs:complexContent>
<xs:extension base="mine:PurchaseOrderBase">
<xs:sequence>
<xs:element name="Header" type="mine:PurchaseOrderItemBase"/>
<xs:element name="Body" type="mine:PurchaseOrderItemBase"/>
<xs:element name="Footer" type="mine:PurchaseOrderItemBase"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="AutoPartsPurchaseContract">
<xs:complexType>
<xs:sequence>
<xs:element name="PurchaseOrders" type="mine:AutoPartsPurchaseOrderType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

-- TestXPathNavBug.xml --
<?xml version="1.0" encoding="UTF-8"?>
<AutoPartsPurchaseContract xmlns="http://mytest.ca/mine"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mytest.ca/mine TestXPathNavBugSchema.xsd">
<PurchaseOrders Updatable="true" Name="" PurchaseOrderNumber="">
<Header ItemType="" ItemDescription="" PurchaseOrderNumber="" />
<Body ItemType="" ItemDescription="" PurchaseOrderNumber="" />
<Footer ItemType="" ItemDescription="" PurchaseOrderNumber="" />
</PurchaseOrders>
</AutoPartsPurchaseContract>

-- Program.cs --
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;

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

//Load the xml with its xsd
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://mytest.ca/mine",
@"..\..\TestXPathNavBugSchema.xsd");
settings.ValidationType = ValidationType.Schema;

XmlReader reader = XmlReader.Create(xmlFileName, settings);

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

//Create an XPathNavigator
XPathNavigator xpath = doc.CreateNavigator();

// Start at the top
xpath.MoveToRoot();
//Modify the xml document
TraverseTree(xpath);

//Save
reader.Close();
doc.Save(xmlFileName);

Console.ReadKey();
}

private static void TraverseTree(XPathNavigator xpath)
{
//Process this node
if (xpath.NodeType == XPathNodeType.Element)
{
Console.WriteLine("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.IsNullOrEmpty(xmlType.Name))
Console.WriteLine(" XmlType: {0}", xmlType.Name);

xmlBaseType = xmlType.BaseXmlSchemaType;
if (!string.IsNullOrEmpty(xmlBaseType.Name))
Console.WriteLine(" XmlBaseType: {0}",
xmlBaseType.Name);
}

//These are the attributes we want to populate in the XML
file.
if (xmlBaseType != null
&& !string.IsNullOrEmpty(xmlBaseType.Name)
&& xmlBaseType.Name.Equals("OrderItemBase",
StringComparison.OrdinalIgnoreCase))
{
if (xpath.HasAttributes)
{
//Grab a clone to start navigating at this position
XPathNavigator navAttributes = xpath.Clone();

navAttributes.MoveToFirstAttribute();

do
{
Console.WriteLine(" Attribute: {0}, can edit?
{1}", navAttributes.Name, navAttributes.CanEdit);

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

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

case "purchaseordernumber":
valueToSave =
DateTime.Now.Ticks.ToString();
break;
}

//TODO: This reproduces the bug.
/* If you call the SetValue, then the XmlTypes
are wiped out on
* subsequent Moves.
* */
if (!string.IsNullOrEmpty(valueToSave))
navAttributes.SetValue(valueToSave);

} while (navAttributes.MoveToNextAttribute());

}
}
}

//Go down the branch
if (xpath.HasChildren)
{
xpath.MoveToFirstChild();

do
{
TraverseTree(xpath);
} while (xpath.MoveToNext());

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

May 23 '07 #1
2 4527
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.Validity
becomes NotKnown; and the XmlType will need Validity with Valid value to
return the correct type.

To fix this, you need to call XmlDocument.Validate 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.Validity
becomes NotKnown; and the XmlType will need Validity with Valid value to
return the correct type.

To fix this, you need to call XmlDocument.Validate 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
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...
1
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...
4
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...
7
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...
1
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";...
11
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);...
0
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...
0
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...
0
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: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.