Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : Get rid of the namespace


Willie
2/25/2005 6:39:03 AM
I've the following code:

writer.WriteStartDocument();
writer.WriteStartElement("n", "Property", "http://www.aaa.com");
writer.WriteAttributeString("xmlns", "j", null, "http://www.bbb.com");

writer.WriteAttributeString("xmlns", "n", null, "http://www.aaa.com");
writer.WriteElementString("PropertyDescriptionText", "http://www.bbb.com",
"hello");

string prefix = writer.LookupPrefix("http://www.aaa.com");
string prefix2 = writer.LookupPrefix("http://www.bbb.com");

writer.WriteElementString("PropertyUNCode", "http://www.aaa.com", "1234");
writer.WriteStartElement(prefix2, "PropertyStatus", "http://www.bbb.com");
writer.WriteElementString("StatusText", "http://www.bbb.com", "FOUND");
writer.WriteEndElement();
....

I got the following output

<n:Property xmlns:n="http://www.aaa.org">
<j:PropertyDescriptionText
xmlns:j="http://www.bbb.com">Hello</j:PropertyDescriptionText>
<n:PropertyUNCode>1234</n:PropertyUNCode>
<j:PropertyStatus xmlns:j="http://www.bbb.com">
<j:StatusText>FOUND</j:StatusText>
</j:PropertyStatus>

Instead i want the output to be

<n:Property xmlns:n="http://www.aaa.com" xmlns:j="http://www.bbb.com">
<j:PropertyDescriptionText>Hello</j:PropertyDescriptionText>
<n:PropertyUNCode>1234</n:PropertyUNCode>
<j:PropertyStatus>
<j:StatusText>FOUND</j:StatusText>
</j:PropertyStatus>

where xmlns:j is the attribute of the root node and get rid of subsequence
Martin Honnen
2/25/2005 5:49:08 PM


[quoted text, click to view]


[quoted text, click to view]

When I try

XmlTextWriter xmlWriter = new XmlTextWriter(Console.Out);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("n", "Property", "http://www.aaa.com/");
xmlWriter.WriteAttributeString("xmlns", "j",
"http://www.w3.org/2000/xmlns/", "http://www.bbb.com/");
xmlWriter.WriteElementString("PropertyDescriptionText",
"http://www.bbb.com/", "Hello");
xmlWriter.WriteEndDocument();
xmlWriter.Close();

here with .NET 1.1 the output is

<n:Property xmlns:j="http://www.bbb.com/" xmlns:n="http://www.aaa.com/">
<j:PropertyDescriptionText>Hello</j:PropertyDescriptionText>
</n:Property>

so I think that is the way you want it (not complete but you simply need
to add the WriteElementString calls missing for the remaining elements.



--

Martin Honnen
Willie
2/28/2005 6:05:02 AM
Willie
2/28/2005 12:25:11 PM
MemoryStream objStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(objStream,
System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;

writer.WriteStartDocument();
writer.WriteStartElement("n", "Property", "http://www.aaa.com");
writer.WriteAttributeString("xmlns", "j", null, "http://www.bbb.com");

writer.WriteAttributeString("xmlns", "n", null, "http://www.aaa.com");
writer.WriteElementString("PropertyDescriptionText", "http://www.bbb.com",
"hello");

string prefix = writer.LookupPrefix("http://www.aaa.com");
string prefix2 = writer.LookupPrefix("http://www.bbb.com");

writer.WriteElementString("PropertyUNCode", "http://www.aaa.com", "1234");
writer.WriteStartElement(prefix2, "PropertyStatus", "http://www.bbb.com");
writer.WriteElementString("StatusText", "http://www.bbb.com", "FOUND");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();

objStream.Position = 0;

DataSet ds = new DataSet();
XmlTextReader reader = new XmlTextReader(objStream);
ds.ReadXml(reader);
writer.Close();
reader.Close();

Willie
2/28/2005 12:29:08 PM
The code you gave me works in Console.Out, but it doesn't work if i put it in
a dataset. To clarify here is the code:

MemoryStream objStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(objStream,
System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;

writer.WriteStartDocument();
writer.WriteStartElement("n", "Property", "http://www.aaa.com");
writer.WriteAttributeString("xmlns", "j", "http://www.w3.org/2000/xmlns/",
"http://www.bbb.com");
writer.WriteElementString("PropertyDescriptionText", "http://www.bbb.com",
"hello");

string prefix = writer.LookupPrefix("http://www.aaa.com");
string prefix2 = writer.LookupPrefix("http://www.bbb.com");

writer.WriteElementString("PropertyUNCode", "http://www.aaa.com", "1234");
writer.WriteStartElement(prefix2, "PropertyStatus", "http://www.bbb.com");
writer.WriteElementString("StatusText", "http://www.bbb.com", "FOUND");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();

objStream.Position = 0;

DataSet ds = new DataSet();
XmlTextReader reader = new XmlTextReader(objStream);
ds.ReadXml(reader);
writer.Close();
reader.Close();

AddThis Social Bookmark Button