Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

XML File Headache... (C#)

Options
  • 25-10-2013 2:20pm
    #1
    Closed Accounts Posts: 83 ✭✭


    Hi folks,

    I'm having a problem with an XML file, which is after turning into a big huge headache for me. Basically I have an XML file that contains the following text:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="hello">Hello!</string>
    </resources>


    I have C# code in my codebehind page, that I am using to read this file, I've tried every different way that I know, to read this simple bit of text above, into a string, and end up with a string that contains the following:

    <resources>
    <string name="hello">Hello!</string>
    </resources>


    What I am ending up, no matter how I try to do this, which is no use to me, is:

    <resources>
    <string name="hello">Hello!</string>
    </resources>

    Is it possible to just read in the WHOLE ENTIRE XML file, (which includes the <?xml version="1.0" encoding="utf-8"?>
    line and somehow pass this value to a string, so that I end up with a string that contains EXAACTLY the contents of my XML file as I have displayed it in blue above?

    XDocument Sendingxml = XDocument.Load(Server.MapPath("~\\Query.xml"));
    
                requ.Method = "POST";
                //byte[] byteArray = Encoding.UTF8.GetBytes(Sendingxml.Document.ToString());
                byte[] byteArray = Encoding.UTF8.GetBytes(Sendingxml.ToString());
    
                requ.ContentType = "text/xml; encoding='utf-8'";
                // Set the ContentLength property of the WebRequest.
                requ.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = requ.GetRequestStream();
                // Write the data to the request stream.
                //dataStream.Write(byteArray, 0, byteArray.Length);
    
    Thanks in advance for any help with this. The problem I'm having is that the site I am trying to pass this data to, can take other requests as well as XML, so having <?xml version="1.0" encoding="utf-8"?>, in the HTTP request string that is being passed, along with the other fields, is essential.


Comments

  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    If you need to just send the raw file, why don't you just read it contents like any normal file rather than parsing it with XDocument?

    Or do you need to parse/validate the XML too, not just send it's content?

    If you need to use XDocument for some reason, the Save method wit the Stream overload should include the XML declaration (and you can use SaveOptions.DisableFormatting if you need to preserve whitespace).
    http://msdn.microsoft.com/en-us/library/cc838778.aspx


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    If you need to just send the raw file, why don't you just read it contents like any normal file rather than parsing it with XDocument?

    Or do you need to parse/validate the XML too, not just send it's content?

    If you need to use XDocument for some reason, the Save method wit the Stream overload should include the XML declaration (and you can use SaveOptions.DisableFormatting if you need to preserve whitespace).
    http://msdn.microsoft.com/en-us/library/cc838778.aspx

    I've tried just reading it in as a normal file and no matter what I do, I'm getting back just the fields. I won't pretend that XML is my wrong area, I've never really used it before but have to use it now to get this thing working... Thanks for reply btw, I'll check that link you suggested...


  • Registered Users Posts: 6,134 ✭✭✭Talisman


    <?xml version="1.0" encoding="utf-8"?>

    This is the xml declaration - it's not part of the xml document.

    Treat the file as text if you want to preserve the entire content. My C# is a little rusty but it should go something like this:

    [PHP]
    string xml_string;
    using (var reader = new StreamReader(Server.MapPath("~\\Query.xml"))
    xml_string = reader.ReadToEnd();
    [/PHP]


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    I think I'm getting somewhere with the two approaches suggested, have been at this all day and have gone down a Cul de Sac with it!


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    If you want a string of the file contents, File.ReadAllText should be the easiest way to do it. Byte array would be ReadAllBytes.
    string contents = File.ReadAllText(Server.MapPath("~\\Query.xml"));
    // OR
    byte[] contents = File.ReadAllBytes(Server.MapPath("~\\Query.xml"));
    

    If you still have no XML declaration, you should probably check that you are reading the correct file and the file does indeed has an XML declaration. Sounds silly, but we all make silly mistakes.


  • Advertisement
  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Hi folks, I thought I was getting somewhere with this, but no luck, I've tried all the solutions above, but I'm still getting back just the data in the XML file where I need the entire text in the file...

    This is how I have it set up now, and I'm still getting back the data on the button click event, which is useless to me. I've even renamed the XML file as a .txt file to try to get this to read the whole file without fiddling with what it reads before printing it to screen but no luck...
            protected void MyXMLTest_OnClientClick(object sender, EventArgs e)
            {
    
    
    
    
                string DataPost = File.ReadAllText(Server.MapPath("~\\Query.txt"));
    
                Response.Write(DataPost.ToString());
    
    
    
    
            }
    

    I can't understand how two lines of code can cause so much of a headache!

    EDIT: This is ultimately going to be a HTTP XML request with a response, but I've had to break the problem down into smaller parts, to lead me to the code above, where I can't even get the string properly set up, that will be going into the HTTP request...

    Also, I've checked the Query.xml file and it has the code I need to read, but for some reason the compiler is just giving me back the field data in the file and not the complete text in the file, which is what I need...


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    How are you reading the response on the client? Is there a chance that the declaration is being passed down the wire, but isn't be displayed on the client side for whatever reason? Tried using Fiddler or browser network viewer to see what exactly is going down the wire? In the debugger, is the string valid before calling Response.Write?

    From what I know, HttpResponse.Write should just output whatever you feed it, without filtering, modification, etc.

    Maybe try setting content type:
    Response.ContentType = "text/xml";
    

    Long question and answer on how to handle XML:
    http://stackoverflow.com/questions/543319/how-to-return-xml-in-asp-net/2163400#2163400


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Thanks for that Procrastinator, what I tried doing last night was building the XML grammatically, which is creating the XML code and placing it into an XML file from the buttonclick event, but as for getting it into a string, I'm gonna try that now and see how I get on...

    Thanks for the advice so far...


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Got this sorted folks, had to dispense with the approach above and generate the XML programatically in the end, too much messing around with encoding issues with the approach I have been using, but have it resolved now, thanks for the help on thread!


  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    This is basically what you want?
    308gr3b.png

    ooops I guess I was late :)


  • Advertisement
Advertisement