Groups | Blog | Home
all groups > dotnet xml > april 2006 >

dotnet xml : XmlNode adds NamespaceURI


Vivek
4/3/2006 12:00:00 AM
Hello,

I wish to copy an XmlNode in a string variable from a XmlDocument object.
But as I takeout this XmlNode, it adds the NamespaceURI. Why ? Could
somebody please help me ?

Vivek

Martin Honnen
4/3/2006 1:13:31 PM


[quoted text, click to view]


[quoted text, click to view]

node.OuterXml serializes a node to a string.

[quoted text, click to view]

Well why not? The namespaceURI is an essential part of any element or
attribute node, just like the local name to identify the node correctly.
If serialization would not declare namespaces then it would not work
correctly.


--

Martin Honnen --- MVP XML
Vivek
4/3/2006 1:24:51 PM
OK. But I want to deserialize this node with serializer.deserialize. And it
gives the exception that "<Nodename xmlns="thisNamespace"> was not expected.


"Martin Honnen" <mahotrash@yahoo.de> a écrit dans le message de news:
%238Ddn%23wVGHA.1236@TK2MSFTNGP11.phx.gbl...
[quoted text, click to view]

Martin Honnen
4/3/2006 2:57:55 PM


[quoted text, click to view]

Provide some more details on the .NET types used, the serialization
attributes you have and why/how you get (serialize?) the node first to
have the wrong namespace.

--

Martin Honnen --- MVP XML
Vivek
4/3/2006 4:19:49 PM
Here is the code :
try
{
// serialise to object
XmlSerializer serializer = new
XmlSerializer(typeof(childactivitiesactivity)); //I use
childactivitiesactivity in place of "child" to get only this branch
sreader = new StringReader(node); // read xml data
reader = new XmlTextReader(sreader); // create reader
// convert reader to object
facture =
(childactivitiesactivity)serializer.Deserialize(reader);
}
catch
{
throw new InvalidOperationException();
}
finally
{
if (sreader != null) sreader.Close();
if (reader != null) reader.Close();
}

the xml file looks like this,

<child>
<profile></profile>
<adress></adress>
<activities>
<activity></activity>
<activity></activity>
</activities>
</child>

I have created the classes with xsd.exe. It has created a table of
activities[] to fill up with activity nodes. While I deserialize, it gives a
table activities with dimensions=0. As I found, on internet that its a bug
of .net, I wanted to fetch the table like the code as above, i.e., passing a
table of "childactivitiesactivity" directly. now, I have started having a
doubt if ever I can deserialize this branch only instead of the whole tree.
Otherwise, I am thinking to use XmlDocument, if it does not work.

Vivek

Chris Lovett
4/5/2006 12:11:10 AM
Please also post the class. Your sample XML probably has a namespace
declaration too right? So try adding an
[System.Xml.Serialization.XmlRoot(Namespace="thisNamespace")]attribute on
your class. Then the serializer should be happy.

[quoted text, click to view]

Vivek
4/7/2006 12:00:00 AM
Sorry for the delay. Here is the code for this class, which I created with
xsd.exe from XSD file.






//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.42
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//
namespace Esvicom.FichierXml {
using System.Xml.Serialization;


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.esvicom.org/enfants",
IsNullable=false)]
public partial class enfant {

private object[] itemsField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("adresse_placeur",
typeof(enfantAdresse_placeur))]
[System.Xml.Serialization.XmlElementAttribute("factures",
typeof(enfantFacturesFacture[]))]
[System.Xml.Serialization.XmlElementAttribute("profil",
typeof(enfantProfil))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class enfantAdresse_placeur {

private string[] adresseField;

private string code_postalField;

private string villeField;

private string departmentField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("adresse",
IsNullable=true)]
public string[] adresse {
get {
return this.adresseField;
}
set {
this.adresseField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="positiveInteger")]
public string code_postal {
get {
return this.code_postalField;
}
set {
this.code_postalField = value;
}
}

/// <remarks/>
public string ville {
get {
return this.villeField;
}
set {
this.villeField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="positiveInteger")]
public string department {
get {
return this.departmentField;
}
set {
this.departmentField = value;
}
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class enfantFacturesFacture {

private string nb_jours_totauxField;

private string nb_presencesField;

private string nb_absencesField;

private string presencesfacturesField;

private string absencesfacturesField;

private decimal montant_presencesField;

private decimal montant_absencesField;

private decimal montant_totalField;

private System.DateTime dateField;

private string periodeField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="positiveInteger")]
public string nb_jours_totaux {
get {
return this.nb_jours_totauxField;
}
set {
this.nb_jours_totauxField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="positiveInteger")]
public string nb_presences {
get {
return this.nb_presencesField;
}
set {
this.nb_presencesField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")]
public string nb_absences {
get {
return this.nb_absencesField;
}
set {
this.nb_absencesField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="positiveInteger")]
public string presencesfactures {
get {
return this.presencesfacturesField;
}
set {
this.presencesfacturesField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")]
public string absencesfactures {
get {
return this.absencesfacturesField;
}
set {
this.absencesfacturesField = value;
}
}

/// <remarks/>
public decimal montant_presences {
get {
return this.montant_presencesField;
}
set {
this.montant_presencesField = value;
}
}

/// <remarks/>
public decimal montant_absences {
get {
return this.montant_absencesField;
}
set {
this.montant_absencesField = value;
}
}

/// <remarks/>
public decimal montant_total {
get {
return this.montant_totalField;
}
set {
this.montant_totalField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="date")]
public System.DateTime date {
get {
return this.dateField;
}
set {
this.dateField = value;
}
}

AddThis Social Bookmark Button