0

Microsoft SEO ツールキットによって次の形式で生成された XML ファイルがあります。

<?xml version="1.0" encoding="utf-8"?>
<urls>
<url url="First URL">
<violations>
  <violation code="HasBrokenLinks" url2="First URL - 1st Broken Link" />
</violations>
</url>
<url url="Second URL">
<violations>
  <violation code="HasBrokenLinks" url2="Second URL - 1st Broken Link" />
  <violation code="HasBrokenLinks" url2="Second URL - 2nd Broken Link" />
  <violation code="HasBrokenLinks" url2="Second URL - 3rd Broken Link" />
  <violation code="HasBrokenLinks" url2="Second URL - 4th Broken Link" />
  <violation code="HasBrokenLinks" url2="Second URL - 5th Broken Link" />
  <violation code="HasBrokenLinks" url2="Second URL - 6th Broken Link" />
</violations>
</url>
</urls>

私は ac# app で結果を解析し、次のように出力しようとしています:

URL:  First URL
Broken Links: First URL - 1st Broken Link

URL: Second URL
Broken Links: Second URL - 1st Broken Link
Second URL - 2nd Broken Link
Second URL - 3rd Broken Link
Second URL - 4th Broken Link
Second URL - 5th Broken Link
Second URL - 6th Broken Link

次のように定義されたクラスがあります。

public class WebpageErrors
{
    public String SourceURL { get; set; }
    private static List<string> BrokenLinkList = new List<string>();

    public void BrokenLinkStore(string BrokenLink)
    {
        BrokenLinkList.Add(BrokenLink);
    }
    public List<string> BrokenLinkReturner
    {
        get { return BrokenLinkList; }
    }

}

次に、xml を反復処理することから始めます。

        // Generate an array to store a list of URLs
        List<WebpageErrors> errorList = new List<WebpageErrors>();

        // File to open up, can be an URL too
        string XmlFileUrl = @path;
        using (XmlReader reader = new XmlTextReader(XmlFileUrl))
        {
            //Define a new object to store errors in
            WebpageErrors Error = new WebpageErrors();

            // Loop the reader, till it cant read anymore
            while (reader.Read())
            {
                // An object with the type Element was found.
                if (reader.NodeType == XmlNodeType.Element)
                {

                    // Check name of the node and write the contents in the object accordingly.
                    if (reader.Name == "url")
                    {
                        //Define a new object to store errors in
                        Error = new WebpageErrors();

                        Error.SourceURL = reader["url"];
                    }

                    // Check name of the node and write the contents in the object accordingly.
                    if (reader.Name == "violation")
                    {
                        // Check name of the node and write the contents in the object accordingly.
                        if (reader["code"] == "HasBrokenLinks")
                        {
                            Error.BrokenLinkStore(reader["url2"]);

                        }
                    }
                } 
                else if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (Error.BrokenLinkReturner.Count > 0) 
                    {
                        errorList.Add(Error);
                    }
                }


            }
        }
        return errorList;

その後、エラーのリストを反復処理して出力します。

    private static void PrintErrors(List<WebpageErrors> Errors)
    {

        StringBuilder Output = new StringBuilder();

        for (int i = 0; i < Errors.Count; i++)
        {
            Output.Append("Source URL: " + Errors[i].SourceURL + Environment.NewLine);

            List<string> BrokenLinkList = Errors[i].BrokenLinkReturner;
            foreach (String BrokenLink in BrokenLinkList)
            {
                Output.Append("Broken Link: " + BrokenLink + Environment.NewLine);
            }

            Output.Append(Environment.NewLine);
        }

私は何か違うものを得ていますが、期待される出力を得る代わりに:

 Source URL: First URL
 Broken Link: First URL - 1st Broken Link
 Broken Link: Second URL - 1st Broken Link
 Broken Link: Second URL - 2nd Broken Link
 Broken Link: Second URL - 3rd Broken Link
 Broken Link: Second URL - 4th Broken Link
 Broken Link: Second URL - 5th Broken Link
 Broken Link: Second URL - 6th Broken Link

 Source URL: First URL
 Broken Link: First URL - 1st Broken Link
 Broken Link: Second URL - 1st Broken Link
 Broken Link: Second URL - 2nd Broken Link
 Broken Link: Second URL - 3rd Broken Link
 Broken Link: Second URL - 4th Broken Link
 Broken Link: Second URL - 5th Broken Link
 Broken Link: Second URL - 6th Broken Link

 Source URL: Second URL
 Broken Link: First URL - 1st Broken Link
 Broken Link: Second URL - 1st Broken Link
 Broken Link: Second URL - 2nd Broken Link
 Broken Link: Second URL - 3rd Broken Link
 Broken Link: Second URL - 4th Broken Link
 Broken Link: Second URL - 5th Broken Link
 Broken Link: Second URL - 6th Broken Link

 Source URL: Second URL
 Broken Link: First URL - 1st Broken Link
 Broken Link: Second URL - 1st Broken Link
 Broken Link: Second URL - 2nd Broken Link
 Broken Link: Second URL - 3rd Broken Link
 Broken Link: Second URL - 4th Broken Link
 Broken Link: Second URL - 5th Broken Link
 Broken Link: Second URL - 6th Broken Link

 Source URL: Second URL
 Broken Link: First URL - 1st Broken Link
 Broken Link: Second URL - 1st Broken Link
 Broken Link: Second URL - 2nd Broken Link
 Broken Link: Second URL - 3rd Broken Link
 Broken Link: Second URL - 4th Broken Link
 Broken Link: Second URL - 5th Broken Link
 Broken Link: Second URL - 6th Broken Link

なぜ私の出力がこんなに台無しになっているのか理解できないようです。WebpageErrors オブジェクトの作成と何か関係があるのでしょうか? 誰かが私が間違っていることを理解するのを手伝ってくれますか?

ありがとうブラッド

4

1 に答える 1

0

私の最初の問題は、私が望んでいないことのようです:

private static List<string> BrokenLinkList = new List<string>();

むしろ:

private List<string> BrokenLinkList = new List<string>();

(私のクラスの static 宣言なしで)。これにより、違反の共有リストではなく、オブジェクトごとに一意のリストが作成されます。

2番目の問題は、次のことでした。

else if (reader.NodeType == XmlNodeType.EndElement)

これは、予想よりも多くの EndElements に一致していました。代わりに、次のように変更する必要がありました。

else if (reader.Name == "url" && reader.NodeType == XmlNodeType.EndElement)

要素がurlであり、EndElementでもある場合にのみ一致します

于 2013-11-05T13:19:03.030 に答える