locked
LinqToXMl RRS feed

  • Question

  • static void InsertData(Student student)
            {
                try
                {
                 
                    XDocument xdoc = XDocument.Load("Data.xml");
                    XElement elem = new XElement("Student",
                        new XAttribute("RollNumber", student.RollNumber),
                        new XAttribute("Name", student.Name));
                    xdoc.Add(elem);
                    xdoc.Save(/*WhatNeedHere???*/);
                }
                catch
                {
                    MessageBox.Show("Error");
                }
                
                
            
            }

    Plz Help, with stream.need to past Path to xml file how i need to do it in silverlight

    static void InsertData(Student student)

            {

                try

                {

                 

                    XDocument xdoc = XDocument.Load("Data.xml");

                    XElement elem = new XElement("Student",

                        new XAttribute("RollNumber", student.RollNumber),

                        new XAttribute("Name", student.Name));

                    xdoc.Add(elem);

                    xdoc.Save(/*WhatNeedHere???*/);

                }

                catch

                {

                    MessageBox.Show("Error");

                }

                

                

            

            }

    Thursday, June 2, 2011 3:50 AM

All replies

  • Hi,

    If you want to save the file locally you could use the SaveFileDialog to prompt the user where to save the file and then use the stream from that file to save the file. For example

    SaveFileDialog textDialog = new SaveFileDialog();
    
       bool? dialogResult = textDialog.ShowDialog();
        if (dialogResult == true)
        {
            using (System.IO.Stream fileStream = textDialog.OpenFile())
    {
    xdoc.Save(fileStream);
    }
        }
    
    

    Or if you could create a new memorystream and save to that if you want to transfer the document to a service in some way.

    Cheers,

    Darren

    Thursday, June 2, 2011 4:08 AM
  • I need to do this not with Dialog but with  Stream

    Thursday, June 2, 2011 4:17 AM
  • If you want to be able to access the local file system without prompting a dialog you will need to be running out-of-browser with elevated trust.

    Check this http://weblogs.asp.net/nmarun/archive/2009/11/27/local-file-access-silverlight-4.aspx

    Thursday, June 2, 2011 5:54 AM
  • How to use Stream  class  here to use Xdoc.Save(Stream); ????Cry

    Thursday, June 2, 2011 6:16 AM
  • I've taken your code sample and altered it slightly as I don't have your Xml Document but the concept should be the same

            static void InsertData(Student student)
            {
                try
                {
    
                    XDocument xdoc = XDocument.Parse("<Test></Test>");
                    XElement elem = new XElement("Student",
                        new XAttribute("RollNumber", student.RollNumber),
                        new XAttribute("Name", student.Name));
                    ((XElement)xdoc.FirstNode).Add(elem);
    
                    using (MemoryStream ms = new MemoryStream())
                    {
                        xdoc.Save(ms);
                    }
                }
                catch
                {
                    MessageBox.Show("Error");
                }
    
    
    
            }


     

    MemoryStream creates an internal Stream. You will be able to use MemoryStream to access the underlying stream.

    Thursday, June 2, 2011 6:34 AM
  • Seems like your objective is different;

    Do you need the class to be converted to an XML and then to a Stream from that XML?

    MemoryStream ms = new MemoryStream(); 
    XmlWriterSettings xws = new XmlWriterSettings(); 
    xws
    .OmitXmlDeclaration = true; 
    xws
    .Indent = true; 
     
    using (XmlWriter xw = XmlWriter.Create(ms, xws)) 
    { 
       
    XDocument doc = new XDocument( 
           
    new XElement("Child", 
               
    new XElement("GrandChild", "some content") 
           
    ) 
       
    ); 
        doc
    .WriteTo(xw); 
    } 
     

     

     
    Thursday, June 2, 2011 6:35 AM
  • No, i have textbox1 (Name) and textbox2(RollNumber) I put some value in this textboxes and it must create  a new element in Data.xml file like <Student Name="someValue" RollNumber="somevalue">

    Thursday, June 2, 2011 7:06 AM
  • If it is binded to a class, then there is a simple method to get the XMLString!

    /// <summary>
            /// Serialize object (Class or list) to XML
            /// </summary>
            /// <param name="objectToSerialize">Class or collection to serialize</param>
            /// <returns></returns>
            public static string SerializeToXMLString(object objectToSerialize)
            {
                XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                // There is no need for the namespace... But this will be useful at times
                // nameSpaces.Add("ns", "http://example.uri.here");            
                XmlSerializer serializer = new XmlSerializer(objectToSerialize.GetType());
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.OmitXmlDeclaration = true;
    
                StringWriter stringWriter = new StringWriter();
                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
                {
                    serializer.Serialize(xmlWriter, objectToSerialize, nameSpaces);
                }
    
                return stringWriter.ToString();
            }

    Else, you may need to create it...

    What do u mean by save? Saving it to the server?
     

    Thursday, June 2, 2011 7:15 AM
  • Save -mean that this will be save in XML file.

    or  new XElement will be added to xml file

    Thursday, June 2, 2011 7:22 AM
  • Where are you loading the XML file from?

    Friday, June 3, 2011 8:54 AM
  • from SilverlightAlpplication, but i found that  i must load it from asp.net host/ClientBin and made webservice, right?

    Friday, June 3, 2011 8:59 AM
  • Yes, as you are accessing a file located on the web server you will need a way to push/pull the file to/from the silverlight client as that runs locally in the browser.

    The easiest way to do this would be to create a web service with methods on it that allow you to retrieve the file and send the altered file back to the web  server.

    Friday, June 3, 2011 9:17 AM
  • Can y link some method examples, to see how can i do this. plz.

    Friday, June 3, 2011 10:00 AM