【问题标题】:How to iterate all nodes?如何迭代所有节点?
【发布时间】:2016-06-17 14:46:58
【问题描述】:

我正在尝试产生以下输出:

<article> <status> </status> ....</article>
<article> <status> </status> ....</article>

我在循环逻辑方面几乎不需要任何帮助 - 任何我可能出错的建议。我尝试使用“for”循环,但未能产生所需的输出。请指教。谢谢你。

public static string createArticleALL()
    {

        XElement xeRoot = new XElement("article");
        XDocument xDoc = new XDocument(xeRoot);

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["###"].ConnectionString))

        {
            con.Open();

            using (SqlCommand command = new SqlCommand("####", con))
            {

                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    string title = reader.GetString(0);  
                    string body = reader.GetString(4);

                    string pub = reader["publication_id"].ToString();
                    string iss = reader["issue_id"].ToString();
                    string sid = reader["STORYID"].ToString(); 


                    string c = url(title, pub, iss, sid);

        DateTime dt = DateTime.Today;

      foreach (XElement element in xDoc.Descendants("article"))
      {

        XElement xeStatus = new XElement("status", "Approved");
        xeRoot.Add(xeStatus);

        XElement xeTitle = new XElement("title", title);
        xeRoot.Add(xeTitle);

        XElement xeSubTitle = new XElement("subtitle", title);
        xeRoot.Add(xeSubTitle);

        XElement xeSynopsis = new XElement("synopsis", body + "...");
        xeRoot.Add(xeSynopsis);

        XElement xeURL = new XElement("url", c);
        xeRoot.Add(xeURL);

        XElement xeDisplayDate = new XElement("display_date", dt);
        xeRoot.Add(xeDisplayDate);


      }


                }
            }


        return xDoc.ToString();
        }
        return null;

    }

【问题讨论】:

  • 1) 您的代码当前产生什么输出? 2) 您想要的输出不是有效的 XML。有效的 XML 必须有一个 root element。您不能使用XDocument 生成无效的 XML。

标签: c# xml xelement


【解决方案1】:

清理了这个。您必须创建一个article 节点,并将其添加到每个while 循环的文档中。假设您没有在其他地方使用标题内容,我删除了多余的部分。

XDocument xDoc = new XDocument(new XElement("Root"));
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["###"].ConnectionString))
{
    con.Open();

    using (SqlCommand command = new SqlCommand("####", con))
    {
        XElement article;
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            xDoc.Add(article = new XElement("article"));
            article.Add(new XElement("status", "Approved"));
            string title;
            article.Add(new XElement("title", title = reader.GetString(0)));
            article.Add( new XElement("subtitle", title));
            article.Add(new XElement("synopsis", reader.GetString(4) + "..."));

            string pub = reader["publication_id"].ToString();
            string iss = reader["issue_id"].ToString();
            string sid = reader["STORYID"].ToString(); 

            string c = url(title, pub, iss, sid);
            article.Add(new XElement("url", c));

            article.Add(new XElement("display_date", DateTime.Today));
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    相关资源
    最近更新 更多