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

Create classes for events?
Masked Textbox for Double?
Resuming spotify/radio after MediaPlayerLauncher's media is finished - WP7
HttpWebRequest + Windows Auth - NetworkCredential just doesn't work [duplicate]
How to add new string to Registry Entry of type REG_MULTI_SZ?
ASP.Net MVC3 application not compiling due to unrecognised escape sequence in Regular Expression annotation
read specific line this xml?
.xls file generated by c# code fails to open in ipad and tablets
The call is ambiguous between the following methods or properties:
Trying to get the value of a text box
Reading numbers from CSV as strings [duplicate]
find a client Side event of radGrid or Div that accure before any postback in page
Streaming Static/Live media online smoothly in ASP.NET/C#
How can I generate the URL for a controller/view, from within a separate controller?
passing parameters to Castle Windsor Typed Factory
Save values between 'Steps' in Wizard ASP.NET

Categories

HOME
twitter-bootstrap
macros
alasql
leaflet
network-programming
service
glsl
reference
where
okhttp3
richtextbox
gnuradio
box-api
wkhtmltopdf
jpql
guidewire
ng2-charts
console-application
simd
ibatis
windows-phone
civicrm
summernote
custom-component
webgl-earth
aws-iot
rvm
oracle-golden-gate
core-animation
utc
r-factor
selenium3
oracle-nosql
circle
incapsula
ninja-forms
socketcluster
jks
visio-2010
xamarin-test-cloud
grib
zedgraph
tsc
timex
suitesparse
scanf
vertex-shader
huawei
addressbook
vitamio
system-integration
sbt-proguard
transaction-isolation
skbio
google-app-engine-php
nonblocking
redquerybuilder
dnvm
tooltwist
pdcurses
tidesdk
asp.net-2.0
strcpy
cl.exe
unityvs
pytz
oxygenxml
rpt
behance-api
banana-pi
azure-caching
data-layers
db2-connect
mahara
lastinsertid
grunt-contrib-imagemin
mobile-robots
ie-developer-tools
dd4t
uikit-dynamics
wordbreaker
windows-scheduler
spawn
pageload
reference-manual
moonscript
netbiscuits
big-endian
event-propagation
eeprom
visual-leak-detector
hwnd
conditional-operator
nscharacterset
helios
appointment
calendarextender
content-delivery-network
disk-based

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