I want the XML in format like :
<?xml version="1.0" encoding="UTF-8"?>
<ac:Main xmlns:trip="http://www.test.com/main.xsd"
xmlns:abc="http://www.test.com/Types.xsd"
xmlns:tw="http://www.test.com/TW.xsd"
xmlns:ck="http://www.test.com/CK.xsd"
xmlns:k1="http://www.test.com/K1.xsd"
xmlns:d1="http://www.test.com/D1.xsd"
xmlns:ac="http://www.test.com/Ac.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.test.com/Ac.xsd file:/D:/schema/AC.xsd">
<ac:Records>
<d1:Header>
<abc:FirstElement>firstValue</abc:FirstElement>
</d1:Header>
</ac:Records>
</ac:Main>
So, I wrote code using XmlWriter as following:
using (XmlWriter writer = XmlWriter.Create(path, settings))
{
writer.WriteStartElement("ac", "Main", "xmlns");
writer.WriteAttributeString("xmlns", "trip", null, http://www.test.com/main.xsd");
writer.WriteAttributeString("xmlns", "abc", null, http://www.test.com/Types.xsd");
writer.WriteAttributeString("xmlns", "tw", null, http://www.test.com/TW.xsd");
writer.WriteAttributeString("xmlns", "kc", null, "http://www.test.com/CK.xsd");
writer.WriteAttributeString("xmlns", "k1", null, "http://localhost:8080/K1.xsd");
writer.WriteAttributeString("xmlns", "d1", null, "http://localhost:8080/D1.xsd");
writer.WriteAttributeString("xsi","schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "http://www.test.com/Ac.xsd file:/D:/schema/AC.xsd");
// writer.WriteAttributeString("xmlns", "ac", null, "http://www.test.com/Ac.xsd ");
writer.WriteStartElement("ac", "Records", "xmlns");
writer.WriteStartElement("d1", "Header", "xmlns");
writer.WriteStartElement("abc", "FirstElement", "xmlns");
writer.WriteString("firstValue");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
But uncommenting line writer.WriteAttributeString("xmlns", "ac", null, "http://www.test.com/Ac.xsd "); yields error "The prefix 'ac' cannot be redefined from 'xmlns' to 'http://www.test.com/Ac.xsd ' within the same start element tag."
So, I commment that line as shown in above code and I got output as :
<?xml version="1.0" encoding="utf-8"?>
<ac:Main
xmlns:wctrp="http://www.test.com/main.xsd"
xmlns:abc="http://www.test.com/Types.xsd"
xmlns:tw="http://www.test.com/TW.xsd"
xmlns:ck="http://www.test.com/CK.xsd"
xmlns:k1="http://www.test.com/K1.xsd"
xmlns:d1="http://www.test.com/D1.xsd"
xmlns:ac="http://www.test.com/Ac.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.test.com/Ac.xsd file:/D:/schema/AC.xsd"
xmlns:ac="xmlns">
<ac:Records>
<d1:Header xmlns:hd1="xmlns">
<abc:FirstElement xmlns:iaiabc="xmlns">firstValue</abc:FirstElement>
</d1:Header>
</ac:Records>
</ac:Main>
The unwanted attibutes are
xmlns:ac="xmlns" in element 'Main',
xmlns:hd1="xmlns" in 'd1:Header' and
xmlns:iaiabc="xmlns" in 'abc:FirstElement'.
Any one kindly suggest me what should I do to remove the unwanted attibutes and to get the output in topmost format.
I am new to XmlWriter.