开发者

saving webbrowser url to xml and retriving

开发者 https://www.devze.com 2023-03-09 04:26 出处:网络
I am trying to save my webbrowser controler URL to a xml file but I am getting an issue with certain characters preventing the save.

I am trying to save my webbrowser controler URL to a xml file but I am getting an issue with certain characters preventing the save.

When I open a simple URL like this:

www.saypeople.com

It succesfully saves, however when I want to save webpage url like this:

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

The save fails.

I have checked a lot things and have discovered that my code only does not save when the url contains any of开发者_高级运维 the two characters &<.

Please help me out.

Here is my code...

    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();
    }

and how can i retrive url by searching specific title in xml.

<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>

i want to search and save the url of toad title in string... please help me out... thx


Wrap the URL in a CDATA section like:

<![CDATA[THE URL CONTENT]]>

Your problems result from the fact that you can not used & and < as XML-data, as they have special meanings in XML: & starts an XML entity, < starts an XML tag. So when you need to add & and < as values, it's easiest to used a CDATA section.

EDIT
You may try the following:

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>";


You could use HttpUtility.HtmlEncode(url).


Your problem is here:

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

<, > and & that caused you problems are markup in XML. InnerXML does not escape markup and those characters get written as they are which would result in an invalid XML fragment. For adding the URL, use InnerText instead. It escapes those characters.


To navigate through an XML file, you have to use a navigator as shown here.

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();

If the file is too heavily nested, go for XML serialization.

0

精彩评论

暂无评论...
验证码 换一张
取 消