我如何处理从网页上抓取的特殊字符(xml和字符串)

时间:2012-04-27 20:19:23

标签: c# special-characters

使用HTMLAgility,C#,XML

我正在使用HTMLAgility来抓取一个网页,然后填充一个类结构,然后将其序列化为XML文档。

我正在处理的数据是吉他和弦,因此我需要管理一些特殊字符。

我正在挣扎的特殊字符是“Aº7”,即前面字符串中的中间字符(这意味着在音乐方面有所减少)。

当我从网页上看到字符串时,我在观察窗口的黑色菱形中看到一个问号,这反过来又填充在XML中。

我的选择是 a)适当地处理char,使其以XML形式呈现为字符。 b)将字符串中此char的每个实例转换为单词“dim”

最好的方法是什么,因为char在替换语句中找到(使用char(code))。

我不确定我应该如何解决这个问题。

下面我用来获取数据的代码(为了清楚起见,这是一次完成的功能,一旦我拥有可用格式的数据将永远不会再次使用!,只是为了创建一个xml序列化对象结构而构建)

        public void BuildDBFromWebSite()
    {
        string[] chordKeys = { "A", "A#", "Ab", "B", "Bb", "C", "C#", "D", "D#", "Db", "E", "Eb", "F", "F#", "G", "G#", "Gb" };

        HtmlWeb web = new HtmlWeb();
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

        foreach (string chordKeyName in chordKeys)
        {
            //LOOP THROUGH THE CHORD KEYS
            chordKey theChordKey = new chordKey() { KeyName = chordKeyName };
            _keys.Add(theChordKey);

            //grab the tone page
            doc = web.Load("http://www.scales-chords.com/showchbykey.php?key=" + theChordKey.KeyName);

            HtmlNode chordListTable = doc.DocumentNode.SelectSingleNode("/html/body/div[@id='wrapper']/div[@id='body']/div[@id='left']/div[@id='visit']/table/tbody");            

            //  CHORDS
            HtmlNodeCollection chordRows = chordListTable.SelectNodes("tr");

            for (int i = 2; i < chordRows.Count; i++)
            {
                //LOOP THROUGH THE CHORDS
                Chord theChord = new Chord();

                HtmlNodeCollection chordInfoCells = chordRows[i].SelectNodes("td");
                HtmlNode chordLink = chordInfoCells[0].SelectSingleNode("a[@href]");

                //each of the next 3 cells can contain a bad glyph for diminished chords
                theChord.ChordName = chordInfoCells[0].InnerText;
                theChord.ChordNameText = chordInfoCells[1].InnerText;
                theChord.Family = chordInfoCells[2].InnerText;
                theChord.Importance = chordInfoCells[3].InnerText;

                //HtmlAgilityPack.HtmlAttribute href = chordLink.Attributes["href"];
                //HTMLAgility tries to encode the bad glyph but uses the wrong escape and breaks the href, work around is to manually strip the href myself
                string theURL = chordLink.OuterHtml;
                theURL = theURL.Remove(0,9);
                int startPos = theURL.IndexOf(">") - 1;

                theURL = theURL.Substring(0, startPos);

                const string theBadCode = "º";

                theChord.ChordNameURL = HTMLEncodeSpecialChars(theURL);
                theChordKey.Chords.Add(theChord);
            }

            //VARIATIONS ETC
            foreach (Chord theChord in theChordKey.Chords)
            {
                //grab the tone page
                doc = web.Load("http://www.scales-chords.com/" + theChord.ChordNameURL);

                HtmlNode chordMoreInfoTable = doc.DocumentNode.SelectSingleNode("/html/body/div[@id='wrapper']/div[@id='body']/div[@id='left']/div[@id='visit']/center/table[1]/tbody");
                HtmlNodeCollection chordMoreInfoRows = chordMoreInfoTable.SelectNodes("tr");

                theChord.Notes = chordMoreInfoRows[3].SelectNodes("td")[1].InnerText;
                theChord.Structure = chordMoreInfoRows[4].SelectNodes("td")[1].InnerText;
                theChord.BelongsTo = chordMoreInfoRows[6].SelectNodes("td")[1].InnerText;

                HtmlNodeCollection variationHTML = doc.DocumentNode.SelectNodes("/html/body/div[@id='wrapper']/div[@id='body']/div[@id='left']/div[@id='visit']/center/b");

                for (int iVariation = 1; iVariation < variationHTML.Count; iVariation=iVariation+2)
                {
                    Variation theVariation = new Variation();

                    theVariation.Notation = variationHTML[iVariation].NextSibling.InnerHtml;
                    theVariation.Difficuty = variationHTML[iVariation + 1].NextSibling.InnerText;

                    string[] theStrings = theVariation.Notation.Split(' ');
                    try
                    {
                        theVariation.String1 = theStrings[1];
                        theVariation.String2 = theStrings[2];
                        theVariation.String3 = theStrings[3];
                        theVariation.String4 = theStrings[4];
                        theVariation.String5 = theStrings[5];
                        theVariation.String6 = theStrings[6];
                    }
                    catch (Exception ex)
                    {

                    }
                    theChord.Variations.Add(theVariation);

                    Console.WriteLine(theChord.ChordNameText + " : " + theVariation.Notation);
                }
            }
        }

        this.SaveToDisk("C:\\chords.xml");
    }

感谢

1 个答案:

答案 0 :(得分:1)

您遇到的问题很可能是由于编码造成的。该页面声明他们使用了charset windows-1252,因此更改这样的代码应该有效。

WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.GetEncoding("windows-1252");
doc = web.Load(client.OpenRead("http://www.scales-chords.com/showchbykey.php?key=" + theChordKey.KeyName));

当然,如果您不止一次使用此函数,我会将WebClient实例的声明移出foreach。