Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    95
    Thanks
    14
    Thanked 0 Times in 0 Posts

    How to write correct aspx-page with xml

    Hi
    On my default.aspx-page I have the following code to output xml:
    Code:
    <%@ 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.

  • #2
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    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.

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    95
    Thanks
    14
    Thanked 0 Times in 0 Posts
    My Xml.Output() looks like this:

    Code:
    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?
    Last edited by earni23; 01-13-2011 at 09:15 AM.

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    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.

    Code:
    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.

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    95
    Thanks
    14
    Thanked 0 Times in 0 Posts
    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?



    If I include html the xml-output fails.
    Last edited by earni23; 01-13-2011 at 06:00 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •