12
I have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<DataTable>
<Columns>
<DataColumn xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ColumnName>NomeColuna1</ColumnName>
<TypeName>System.String</TypeName>
</DataColumn>
<DataColumn xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ColumnName>NomeColuna2</ColumnName>
<TypeName>System.String</TypeName>
</DataColumn>
<DataColumn xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ColumnName>NomeColuna3</ColumnName>
<TypeName>System.Nullable`1[System.Decimal]</TypeName>
</DataColumn>
<DataColumn xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ColumnName>NomeColunaN</ColumnName>
<TypeName>System.Nullable`1[System.Decimal]</TypeName>
</DataColumn>
</Columns>
</DataTable>
Each tag <DataColumn>
was previously generated from an individual serialization process.
At the end, all these tags are enveloped in the grouping tag <Columns>
during actual XML recording (I use, in this case, a XmlTextWriter
).
How do not control the serialization of each tag <DataColumn>
, the namespace xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
shall be declared for each of them.
Is there a simple way to, at the end of the construction of the final XML, perform a cleanup to reduce redundancies? The result I hope would be:
<?xml version="1.0" encoding="UTF-8"?>
<DataTable xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Columns>
<DataColumn>
<ColumnName>NomeColuna1</ColumnName>
<TypeName>System.String</TypeName>
</DataColumn>
<DataColumn>
<ColumnName>NomeColuna2</ColumnName>
<TypeName>System.String</TypeName>
</DataColumn>
<DataColumn>
<ColumnName>NomeColuna3</ColumnName>
<TypeName>System.Nullable`1[System.Decimal]</TypeName>
</DataColumn>
<DataColumn>
<ColumnName>NomeColunaN</ColumnName>
<TypeName>System.Nullable`1[System.Decimal]</TypeName>
</DataColumn>
</Columns>
</DataTable>
The curious thing is that this namespace is not even used in the document. You can post the code that generates the relevant XML?
– Jordão
This namespace is automatically generated by . NET when using serialization by
DataContract
; If serialization is usedXMLSerializer
, this will also occur, but with the aliasxmlns:xsi=...
– Jônatas Hudler
It wouldn’t be something like '<i:Datacolumn [...] />'?
– Butzke
@Butzke: In this case, Jordan you’re right - there would be no need for this namespace. Since I have no control over his generation, and I’ve researched the Gringo OS, it takes a little work to remove it. At least, I would like to know if there is a method in some . NET class (or if someone built something) that performs a "clean" in XML. As I intend to save this XML in the database, I would mind a more streamlined XML.
– Jônatas Hudler
How do you write the tags in Xmltextwriter? With Writeraw?
– Jordão
@Jordan: That’s right, that’s right!
– Jônatas Hudler
Note that in the expected result example you didn’t eliminate redundancy in the expected location, you actually changed the namespace. Imagine if inside the <Columns> or <Datatable> was added another element other than Datacolumn, it would assume the mentioned namespace, which might not be correct. (maybe for your specific use yes, but it’s not something that an application could simply "guess" how to optimize)
– Bacco