2011-05-31 54 views
1

我試圖將我的webbrowser控制器URL保存爲xml文件,但我遇到了某些防止保存的字符的問題。將webbrowser url保存到xml並重試

當我打開一個簡單的網址是這樣的:

www.saypeople.com

它成功地撲救,但是當我要保存網頁的網址是這樣的:

http://scholar.google.com.pk/scholar?as_q=filetype:pdf +transistor+ AND&num=10&btnG=Search+Scholar&as_epq=&as_oq=unknown+unclear&as_eq=&as_occt=any&as_sauthors=+ &as_publication=+ &as_ylo=&as_yhi=&as_sdt=1.&as_sdtp=on&as_sdtf=&as_sdts=5&hl=en

保存失敗。

我檢查了很多東西,發現我的代碼只在URL包含任何兩個字符&<時纔會保存。

請幫我一把。

這裏是我的代碼...

public static DialogResult Show(string Title, String url) 
    { 
     MsgBox = new addfav(); 
     MsgBox.textBox1.Text = Title; 
     MsgBox.textBox2.Text = url; 
     MsgBox.ShowDialog(); 
     return result; 


    } 
    const string dataxml = "data.xml"; 

    private void button1_Click(object sender, EventArgs e) 
    { 
     //textBox2.Text containing webpage url 
     //textBox1.Text containing webpage title 

     try 
     { 
      XmlTextReader reader = new XmlTextReader(dataxml); 
      XmlDocument doc = new XmlDocument(); 
      doc.Load(reader); 
      reader.Close(); 
      XmlNode currNode; 

      XmlDocumentFragment docFrag = doc.CreateDocumentFragment(); 
      docFrag.InnerXml = "<fav>" + "<Title>" + textBox1.Text + "</Title>" + "<url>"+ textBox2.Text + "</url>" + "</fav>"; 
      // insert the availability node into the document 
      currNode = doc.DocumentElement; 
      currNode.InsertAfter(docFrag, currNode.LastChild); 
      //save the output to a file 
      doc.Save(dataxml); 
      this.DialogResult = DialogResult.OK; 
      MessageBox.Show("Sucessfully Added"); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Exception: {0}", ex.ToString()); 
      this.DialogResult = DialogResult.Cancel; 
     } 


     MsgBox.Close(); 
    } 

和 我怎樣才能通過XML搜索特定標題retrive網址。

<fav> 
<Title>hello</Title> 
<url><![CDATA[http://scholar.google.com.pk/scholar?as_q=filetype:pdf +hello+ AND&num=10&btnG=Search+Scholar&as_epq=&as_oq=unknown+unclear&as_eq=&as_occt=any&as_sauthors=+ &as_publication=+ &as_ylo=&as_yhi=&as_sdt=1.&as_sdtp=on&as_sdtf=&as_sdts=5&hl=en]]></url> 
</fav> 
<fav> 
<Title>toad</Title> 
<url><![CDATA[http://www.sciencedaily.com/search/?keyword=toad+ AND unknown OR unclear]]></url> 
</fav> 

我要搜索並保存在串蟾蜍標題的網址...請幫我... THX

+0

如果你還發布了你所看到的異常,那將會很好。 – 2011-05-31 08:39:22

回答

1

裹在像CDATA節的網址:

<![CDATA[THE URL CONTENT]]> 

您的問題是由於您不能使用&和<作爲XML數據,因爲它們在XML中有特殊含義:&啓動XML實體,<啓動XML標記。因此,當您需要將&和<作爲值時,最容易使用CDATA部分。

編輯
你可以嘗試以下方法:

XmlDocumentFragment docFrag = doc.CreateDocumentFragment(); 
docFrag.InnerXml = "<fav>"; 
docFrag.InnerXml += String.Format("<Title>{0}</Title>", textBox1.Text); 
docFrag.InnerXml += String.Format("<Url><![CDATA[{0}]]></Url>", textBox2.Text); 
docFrag.InnerXml += "</fav>"; 
+0

我嘗試了很多,但沒有得到,如何在我的代碼中使用CreateCDataSection ... – user777304 2011-05-31 09:22:30

+0

請參閱我的編輯 - 這應該有所幫助 – 2011-05-31 11:07:09

+0

非常感謝你的工作。併成功地存儲網址.. – user777304 2011-06-02 06:32:11

0

你可以使用HttpUtility.HtmlEncode(url)

0

你的問題是在這裏:

docFrag.InnerXml = "<fav>" + "<Title>" + textBox1.Text + "</Title>" 
        + "<url>"+ textBox2.Text + "</url>" + "</fav>"; 

<>&導致你的問題是在XML標記。 InnerXML不會轉義標記,並且這些字符會按原樣寫入,這會導致無效的XML片段。要添加URL,請改爲使用InnerText。它逃脫了這些角色。

0

要導航XML文件,您必須使用如此處所示的導航器。

XPathDocument xpathDoc = new XPathDocument([location of the file]); 
XPathNavigator Navigator = xpathDoc.CreateNavigator(); 

String url_nav = "fav/url/text()"; 
XPathNodeIterator url_iterator = Navigator.Select(url_nav); 

String URL_value = url_iterator.Current.Value; 

url_iterator.MoveNext(); 

如果文件嵌套過於嚴重,請進行XML序列化。