c#


XmlCompiledTransform duplicates namespace when replacing element's value


I'm trying to use XslCompiledTransform C# class to transform one xml file into another. However, the namespace is being included a second time in one of my elements (SerialNum). What am I doing wrong?
Here's my C# code:
// Create a reader to read books.xml
XmlReader reader = XmlReader.Create("machine1.xml");
// Create a writer for writing the transformed file.
XmlWriter writer = XmlWriter.Create("machine2.xml");
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("transform.xsl");
// Execute the transformation.
transform.Transform(reader, writer);
Here's my XSL:
<xsl:stylesheet version="1.0" xmlns:cm="http://schemas.datacontract.org/2004/07/CMachines"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<!-- Copy everything not subject to the exceptions below -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<!-- Ignore the disabled element -->
<xsl:template match="cd:Disabled" />
<!-- Reset the value of the serial num element to 0 -->
<xsl:template match="cm:SerialNum">
<SerialNum>0</SerialNum>
</xsl:template>
</xsl:stylesheet>
Input:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMachine xmlns="http://schemas.datacontract.org/2004/07/CMachines" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Machine>
<Name>DellM7600</Name>
<ID>1</ID>
<Type>Laptop</Type>
<Disabled>false</Disabled>
<SerialNum>47280420</SerialNum>
</Machine>
<Machine>
<Name>DellD600</Name>
<ID>2</ID>
<Type>Laptop</Type>
<Disabled>false</Disabled>
<SerialNum>53338123</SerialNum>
</Machine>
</ArrayOfMachine>
Output:
<?xml version="1.0" encoding="utf-8"?><ArrayOfMachine xmlns="http://schemas.datacontract.org/2004/07/CMachines" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Machine>
<Name>DellM7600</Name>
<ID>1</ID>
<Type>Laptop</Type>
<SerialNum xmlns="" xmlns:cm="http://schemas.datacontract.org/2004/07/CMachines">0</SerialNum>
</Machine>
<Machine>
<Name>DellD600</Name>
<ID>2</ID>
<Type>Laptop</Type>
<SerialNum xmlns="" xmlns:cm="http://schemas.datacontract.org/2004/07/CMachines">0</SerialNum>
</Machine>
</ArrayOfMachine>
Desired Output:
<?xml version="1.0" encoding="utf-8"?><ArrayOfMachine xmlns="http://schemas.datacontract.org/2004/07/CMachines" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Machine>
<Name>DellM7600</Name>
<ID>1</ID>
<Type>Laptop</Type>
<SerialNum>0</SerialNum>
</Machine>
<Machine>
<Name>DellD600</Name>
<ID>2</ID>
<Type>Laptop</Type>
<SerialNum>0</SerialNum>
</Machine>
</ArrayOfMachine>
Just replace:
<xsl:template match="cm:SerialNum">
<SerialNum>0</SerialNum>
</xsl:template>
with:
<xsl:template match="cm:SerialNum">
<xsl:copy>0</xsl:copy>
</xsl:template>
What you have now is creating a new SerialNum element in no-namespace - unlike the original, which inherits its parent's namespace. That's why you see the xmlns="" declaration: it shows that the element is in no-namespace, unlike its parent.
The xmlns:cm="http://schemas.datacontract.org/2004/07/CMachines" is simply inherited from the xsl:stylesheet ancestor. You could eliminate it by adding a exclude-result-prefixes="cm" attribute to the xsl:stylesheet element - but it's much simpler just to copy the original SerialNum element with its original namespace and no inheritance from the stylesheet.

Related Links

Implementation of traversing of chunk partition in PLINQ
How to create an async pagination
Force Deserialize DateTime in a property of type object
CarouselView with a different template for each slide in Xamarin forms
How can i use linq for parsing string datetime to real datetime?
RFC server with .Net connector Gateway not matching
C# is it possible to add multiple values to an item.insert
How to Reference File in Unit Test?
XUnit and MSTest with ExpectedException Returning Different Results
Ajax.BeginForm and HttpPostedFileBase
Unbound DataGridView causing causing index was out of range error
how to publish c# database project
WPF style DatePicket Prev & Next buttons
Azure Service Bus DeadLetter QUEUE
Windows application and database connection
Large data table to multiple csv files of specific size in .net

Categories

HOME
asp.net
ide
webvr
popover
android-ndk
stdout
xss
richtextbox
country-codes
sahi
obd-ii
mongoimport
vichuploaderbundle
desktop-application
jetbrains
xmlhttprequest
comma
android-notifications
dompdf
css-float
sonarlint
dev-c++
aws-iot
v4l2
aws-certificate-manager
deployment-descriptor
hierarchical-data
drombler-fx
resx
restangular
pushpad
xlib
ios9
openoffice-impress
jboss-arquillian
storefront
node-soap
odp.net
android-geofence
sdl-2
respect-validation
lighting
extern
realloc
blueimp
risc
automapper-4
visualizer
node-java
ios-keyboard-extension
msiexec
nls
spring-social-twitter
copy-constructor
qudpsocket
gevent
firebase-job-dispatcher
sqldataadapter
syncano
hexagonal-tiles
nmock
playn
eula
data-representation
jeet-grid
jad
applinks
system.io.packaging
zoneminder
mvcgrid
telerik-appbuilder
hotlinking
jsmpp
email-headers
class-library
carrier
codio
node-amqp
netbeans-plugins
xcode5.1
mobile-robots
objectdatasource
listpicker
dd4t
qmainwindow
coinbase-php
blotter
feincms
idoc
diazo
jmesa
mscorlib
isgl3d
youtube.net-api
lwp
norton
rpxnow

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App