Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Linq and updating data RSS

2 replies

Last post Jun 12, 2008 05:51 AM by Yi-Lun Luo - MSFT

(0)
  • Gabrielle

    Gabrielle

    Member

    106 Points

    146 Posts

    Linq and updating data

    Jun 10, 2008 11:25 PM | LINK

    Here is the code:

    public void SaveContentFromPage(string controlName, string Text)

    {

    XDocument doc = XDocument.Load(@"C:/PageContentData.xml");

    foreach (var c in doc.Descendants("DataItem").Elements("Name"))

    if ((string)c.Element("Name") == controlName)doc.Element("DataItem").Element("Content").ReplaceNodes(Text);

    }

     

    VS says at last line that Object reference not set to an instance of an object.

    What am I doing wrong?

     My XML file is:

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

    <ContentData>

    <DataItem>

    <Name>Header</Name>

    <Content>HELLO JULI! HOW ARE YOU TODAY?</Content>

    </DataItem>

    <DataItem>

    <Name>Header2</Name>

    <Content>HELLO JULIsdfsdfdsfsdf! HOW ARE YOU TODAY?</Content>

    </DataItem>

    <DataItem>

    <Name>Header343</Name>

    <Content>HELLO dfdsJULI! HOW ARE YOU TODAY?</Content>

    </DataItem>

    </ContentData>

  • Leonid Andruhin

    Leonid Andruhin

    Participant

    888 Points

    192 Posts

    Re: Linq and updating data

    Jun 11, 2008 06:39 AM | LINK

    string s = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <ContentData><DataItem> <Name>Header</Name><Content>HELLO JULI! HOW ARE YOU TODAY?</Content> </DataItem><DataItem> <Name>Header2</Name><Content>HELLO JULIsdfsdfdsfsdf! HOW ARE YOU TODAY?</Content> </DataItem><DataItem> <Name>Header343</Name><Content>HELLO dfdsJULI! HOW ARE YOU TODAY?</Content> </DataItem></ContentData>";

    System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(s);

    foreach (var c in doc.Descendants("DataItem"))

    if ((string)c.Element("Name") == controlName)

    c.Element("Content").ReplaceNodes(Text);

    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    System.IO.StringWriter sr = new System.IO.StringWriter(sb);

    doc.Save(sr);

    string ss = sb.ToString();//result

    sr.Close();

  • Yi-Lun Luo - MSFT

    Yi-Lun Luo -...

    All-Star

    25149 Points

    2759 Posts

    Microsoft

    Re: Linq and updating data

    Jun 12, 2008 05:51 AM | LINK

    Hello, you're trying to load a file from the user's local system. For security reasons, this is not allowed. You can, however, open and save a file without user interaction from the isolated storage, or open a file (but not save it) with OpenFileDialog as the user requests, or download and upload a file from the web. Either way, Leonid has shown you how to update the xml content. After that, to save the string to a file in isolated storage, please refer to that quick start. To upload the string to web, you can use WebClient.UploadString, use an HttpWebRequest to do a POST, or call a web service method. On the server side, you save the string to a file.

    shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.