...

View Full Version : How to write correct aspx-page with xml



earni23
01-12-2011, 08:32 PM
Hi
On my default.aspx-page I have the following code to output xml:


<%@ Page Language="C#" %>
<script runat="server" type="text/C#">
protected void Page_Load(object sender, EventArgs e)
{
XMLHandler xmlHandler = new XMLHandler();
xmlHandler.Output();
}
</script>


Beacuse I have no html-syntax, visual studio gives me a warning (element 'html' occurs to few times), but it compiles. If I use html-syntax, there is no output of xml on page. Should I just ignore the warning or is it a better way to go.

pigpen
01-12-2011, 11:07 PM
Is that all your code? What is going into your XML doc?

My guess it that you aren't creating a valid XML document so that is why you are getting the compilation errors. It'll compile of course, but your output does not seem to be valid because it's empty.

earni23
01-13-2011, 09:57 AM
My Xml.Output() looks like this:



public void Output()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;

HttpContext.Current.Response.ContentType = "text/xml";
using (XmlWriter writer = XmlWriter.Create(HttpContext.Current.Response.OutputStream, settings))
{
writer.WriteStartDocument();

for (int i = 0; i < divingSpotList.Count; i++)
{
Marker marker = (Marker)markers[i];

writer.WriteStartElement("marker");
writer.WriteAttributeString("location", marker.GetLocation());
writer.WriteEndElement();
}

writer.WriteEndDocument();
writer.Flush();
}


My works (I get the correct ouput), but it gives me a warning (a warning, not a error) because i have no HTML-syntax(element 'html' occurs to few times). My question is: how do I write the correct aspx.net page that outputs xml, without giving any warnings?

pigpen
01-13-2011, 12:42 PM
Looking at your XMLWriter code, it seems to me that you are missing an element.

Does your XML that you output, look valid? I think you need to add another XML element outside your loop. You need to wrap your individual xml elements into a single parent element, like the plural "markers", and then have your loop output each individual element as "marker", which will be nested inside the "markers" element.



using (XmlWriter writer = XmlWriter.Create(HttpContext.Current.Response.OutputStream, settings))
{
writer.WriteStartDocument();

// add this line, which will be the container/parent element
writer.WriteStartElement("markers");

for (int i = 0; i < divingSpotList.Count; i++)
{
// your loop code
}

// and end your "markers" element with this line
writer.WriteEndElement();

writer.WriteEndDocument();
writer.Flush();



If that doesn't work, then post your XML that is output, so we can take a look if it's malformed.

earni23
01-13-2011, 06:23 PM
Never mind the ouput, it works. There is no problem with that. The problem is that vs gives me a warning: Element 'html' occurs to few times. How do I fix this warning?

http://www.nerena.se/pic.bmp

If I include html the xml-output fails.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum