Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove duplicate attributes from XML with C#
    primarykey
    data
    text
    <p>I am parsing some XML files from a third party provider and unfortunately it's not always well-formed XML as sometimes some elements contain duplicate attributes.</p> <p>I don't have control over the source and I don't know which elements may have duplicate attributes nor do I know the duplicate attribute names in advance.</p> <p>Obviously, loading the content into an <code>XMLDocument</code> object raises an XmlException on the duplicate attributes so I though I could use an <code>XmlReader</code> to step though the XML element by element and deal with the duplicate attributes when I get to the offending element.</p> <p>However, the <code>XmlException</code> is raised on <code>reader.Read()</code> - before I get a chance to insepct the element's attributes. </p> <p>Here's a sample method to demonstrate the issue:</p> <pre><code>public static void ParseTest() { const string xmlString = @"&lt;?xml version='1.0'?&gt; &lt;!-- This is a sample XML document --&gt; &lt;Items dupattr=""10"" id=""20"" dupattr=""33""&gt; &lt;Item&gt;test with a child element &lt;more/&gt; stuff&lt;/Item&gt; &lt;/Items&gt;"; var output = new StringBuilder(); using (XmlReader reader = XmlReader.Create(new StringReader(xmlString))) { XmlWriterSettings ws = new XmlWriterSettings(); ws.Indent = true; using (XmlWriter writer = XmlWriter.Create(output, ws)) { while (reader.Read()) /* Exception throw here when Items element encountered */ { switch (reader.NodeType) { case XmlNodeType.Element: writer.WriteStartElement(reader.Name); if (reader.HasAttributes){ /* CopyNonDuplicateAttributes(); */} break; case XmlNodeType.Text: writer.WriteString(reader.Value); break; case XmlNodeType.XmlDeclaration: case XmlNodeType.ProcessingInstruction: writer.WriteProcessingInstruction(reader.Name, reader.Value); break; case XmlNodeType.Comment: writer.WriteComment(reader.Value); break; case XmlNodeType.EndElement: writer.WriteFullEndElement(); break; } } } } string str = output.ToString(); } </code></pre> <p>Is there another way to parse the input and remove the duplicate attributes without having to use regular expressions and string manipulation?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload