I have an .XML file (This is a LOG that my program made) with this text in it :
<?xml version="1.0" encoding="utf-8"?>
<PsnRecords>
<PsnRecord>
<Names></Names>
<PsnUrl>http://gs2.ww.prod.dl.playstation.net/gs2/ppkgo/prod/CUSA05330_00/108/f_acb1a312a982305e284718898b3dade6afb395e6718d836b1d7b1e1aa1873800/f/EP0953-CUSA05330_00-BRAWLHALLAEUROPE-A0403-V0100-DP.pkg</PsnUrl>
<LocalUrl>C:\Users\Betrisa\Desktop\Shared\EP0953-CUSA05330_00-BRAWLHALLAEUROPE-A0403-V0100-DP.pkg</LocalUrl>
<isLixian>false</isLixian>
<LixianUrl></LixianUrl>
</PsnRecord>
<PsnRecord>
<Names></Names>
<PsnUrl>http://gs2.ww.prod.dl.playstation.net/gs2/ppkgo/prod/CUSA05330_00/108/f_acb1a312a982305e284718898b3dade6afb395e6718d836b1d7b1e1aa1873800/f/EP0953-CUSA05330_00-BRAWLHALLAEUROPE-A0403-V0100.pkg?downloadId=0000015b&du=000000000000015b00e26bd28904ee7f&product=0187&serverIpAddr=192.168.137.1&r=00000000</PsnUrl>
<LocalUrl></LocalUrl>
<isLixian>false</isLixian>
<LixianUrl></LixianUrl>
</PsnRecord>
<PsnRecord>
<Names></Names>
<PsnUrl>http://ic.97f46e00.060798.gs2.sonycoment.loris-e.llnwd.net/gs2/ppkgo/prod/CUSA05330_00/108/f_acb1a312a982305e284718898b3dade6afb395e6718d836b1d7b1e1aa1873800/f/EP0953-CUSA05330_00-BRAWLHALLAEUROPE-A0403-V0100.pkg?downloadId=0000015b&du=000000000000015b00e26bd28904ee7f&product=0187&serverIpAddr=192.168.137.1&r=00000001</PsnUrl>
<LocalUrl></LocalUrl>
<isLixian>false</isLixian>
<LixianUrl></LixianUrl>
</PsnRecord>
</PsnRecords>
I want to get all URL links and save them to a .TXT file. I tried in 2 ways but they did not work:
Way 1 : using Split (Result is : Url )
private void button1_Click(object sender, EventArgs e)
{
string paths = Application.StartupPath + @"\DataFiles\DataHistory.xml";
string resPaths = Application.StartupPath + @"\DataFiles\Links.txt";
StreamWriter urlsWrite = File.CreateText(resPaths);
var text = System.IO.File.ReadAllText(paths);
var links = text.Split("\t\n ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Where(s => s.StartsWith("<PsnUrl>http://") || s.StartsWith("<PsnUrl>https://"));
foreach (string s in links)
{
urlsWrite.WriteLine(s);
}
}
Way 2 : using Regex (Result is Nothing !!)
private void button1_Click(object sender, EventArgs e)
{
string paths = Application.StartupPath + @"\DataFiles\DataHistory.xml";
string resPaths = Application.StartupPath + @"\DataFiles\Links.txt";
StreamWriter urlsWrite = File.CreateText(resPaths);
var text = System.IO.File.ReadAllText(paths);
var regex = new Regex(@"\b(?:http?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection mactches = regex.Matches(text);
foreach (string matc in links)
{
text = text.Replace(matc.Value, "<PsnUrl>"+matc.Value+"</PsnUrl>");
urlsWrite.WriteLine(mats);
}
}
I want a .TXT file with clean URLs in it, like:
https://xxxxxxxxxxxxxx
http://xxxxxxxxxxxxxx
https://xxxxxxxxxxxxxx
https://xxxxxxxxxxxxxx
https://xxxxxxxxxxxxxx
https://xxxxxxxxxxxxxx
What am I doing wrong?