2016-06-17 2 views
0

Я пытаюсь произвести следующий вывод:Как перебирать все узлы?

<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; 

    } 
+0

1) Какой результат производит ваш код в настоящее время? 2) Выбранный вывод недействителен XML. Действительный XML должен иметь один [корневой элемент] (https://en.wikipedia.org/wiki/Root_element). Вы не можете использовать 'XDocument' для создания недопустимого XML. – dbc

ответ

0

Очистить это. Вам нужно создать узел 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)); 
     } 
    } 
} 
Смежные вопросы