问题描述
如标题中所述。我正在制作一个C#脚本,该脚本可以从我随时可以编辑的外部文件中将书签导入Google Chrome。导入新条目并检测现有条目可以很好地工作,但是我无法检测到某个书签的链接是否被更改。
例如:名称为“ x”的现有书签具有URL:“ http://test.com”,而我的XML文件具有名称为“ x”的相同条目,但具有另一个URL:“ https:/ /test.com”。
所以我的问题是如何根据链接的名称检测链接是否被更改。我已经尝试过比较Google书签文件和我的XML中的字符串,但是肯定无法正常工作。
我真的依靠您的帮助,因为我需要这份工作才能工作。我知道这是很难编码的,因此,如果您能为我提供一个更合适的解决方案,那么将不胜感激。
这是我的代码段:
private void ChromeBookmarksButtonTest_Click(object sender,RoutedEventArgs e)
{
//Here we read an XML file
var doc = XDocument.Load(@"testpath\BookmarksTestConfig.xml");
XDocument xdc = XDocument.Parse(doc.ToString());
//Here we get all values of attribute called "link"
var arrNames = xdc.Root.Descendants("favorite").Select(x => x.Attribute("link").Value);
//Here we read the Bookmarks json file
string input = System.IO.File.ReadAllText(@"testtestpath\Bookmarks");
using (StringReader reader = new StringReader(input))
using (JsonReader jsonReader = new JsonTextReader(reader))
{
JsonSerializer serializer = new JsonSerializer();
var o = (JToken)serializer.Deserialize(jsonReader);
//Here we get to all existing bookmarks from the above json file
var allExistingLinks = o["roots"]["bookmark_bar"]["children"];
int i = 1;
int j = 1;
//And in this "double" foreach statement I compare all the values
foreach (var element in doc.Elements("Bookmarks").Elements())
{
Console.WriteLine(i + " entry to the outer loop,link from xml: " + element.Attribute("link").Value.ToString());
foreach (var url in allExistingLinks)
{
Console.WriteLine(j + " entry to the inner loop");
j++;
if (arrNames.Contains(url["url"].ToString()) && url["url"].ToString() == element.Attribute("link").Value.ToString())
{
Console.WriteLine("Link from xml " + element.Attribute("link").Value + " already exists");
break;
}
else if (arrNames.Contains(url["url"].ToString()) && url["url"].ToString() != element.Attribute("link").Value.ToString())
{
Console.WriteLine("New link to be added: " + element.Attribute("link").Value);
}
else if (element.Attribute("name").Value.ToString().Contains(url["name"].ToString()))
{
Console.WriteLine("link " + url["url"].ToString() + " needs to be updated to " + element.Attribute("link").Value.ToString());
return;
}
else
{
Console.WriteLine("Skip user defined link: " + url["url"]);
}
}
i++;
}
}
}
这是我的XML:
<Bookmarks>
<favorite name="yandex" link="https://yandex.ru/"/>
<favorite name="web office - mail" link="https://rambler.ru/"/>
<favorite name="test" link="http://test1.com/"/>
</Bookmarks>
这是json:
{
"checksum": "54bbe69fccf882248399398545067e17","roots": {
"bookmark_bar": {
"children": [ {
"date_added": "13246099257433795","guid": "3e300134-1099-4e46-8c48-afae05ac875a","id": "2","name": "pgone","type": "url","url": "https://rambler.ru/"
},{
"date_added": "13246099257433795","guid": "f35a73a3-a6a4-45c6-a065-1f8433841f1b","id": "3","name": "google","url": "https://google.com/"
},"name": "test","url": "http://test.com/"
}],"date_added": "13246099255116869","date_modified": "13246099257433795","guid": "00000000-0000-4000-a000-000000000002","id": "1","name": "Bookmarks bar","type": "folder"
},"other": {
"children": [ ],"date_added": "13246099255116889","date_modified": "0","guid": "00000000-0000-4000-a000-000000000003","id": "4","name": "Other bookmarks","synced": {
"children": [ ],"date_added": "13246099255116893","guid": "00000000-0000-4000-a000-000000000004","id": "5","name": "Mobile bookmarks","type": "folder"
}
},"version": 1
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)