【问题标题】:Data not Saved in file xml数据未保存在文件 xml 中
【发布时间】:2020-06-02 19:49:59
【问题描述】:

我在 ASP.NET MVC 中有这段代码:我想将 datareader 中的数据保存在 xml 文件中,我使用了如下所示的代码,但文件中只有两行:

<?xml version="1.0" encoding="utf-8"?>
<Centre_Commercial />

我在控制器中的代码:

XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", null));
XElement nodecentre = new XElement("Centre_Commercial");
document.Add(nodecentre);

while (dr.Read())
{
    nodecentre.Add(new XElement("Num_Centre", Convert.ToInt32(dr[0].ToString())));
    nodecentre.Add(new XElement("Num_Centre", dr[1].ToString()));
    nodecentre.Add(new XElement("Num_Centre", Convert.ToDateTime(dr[2].ToString()).Date));
    nodecentre.Add(new XElement("Num_Centre", int.Parse(dr[3].ToString())));
    nodecentre.Add(new XElement("Num_Centre", int.Parse(dr[4].ToString())));
    nodecentre.Add(new XElement("Num_Centre", Convert.ToDateTime(dr[5].ToString())));
    nodecentre.Add(new XElement("Num_Centre", dr[6].ToString()));
    nodecentre.Add(new XElement("Num_Centre", dr[7].ToString()));
    nodecentre.Add(new XElement("Num_Centre", int.Parse(dr[8].ToString())));

    document.Save(Server.MapPath(@"~/App_Data/test.Xml"));
}

con.Close();

提前致谢

【问题讨论】:

  • Kamilia,请更新您的原始帖子并添加所需的 XML 输出。
  • @YitzhakKhabinsky 我想从数据中获取文件 xml 中的所有数据我只得到两行?在脚本上方
  • 卡米利亚,我明白发生了什么。请分享您想要的输出 XML

标签: xml asp.net-mvc linq-to-xml


【解决方案1】:

总是返回两行:

<?xml version="1.0" encoding="utf-8"?>
<Centre_Commercial />

在下面更新我的代码:

using (SqlConnection con = new SqlConnection("Server=DESKTOP-DEOG0BL\\SQLEXPRESS;database=db_WCS;integrated security=yes"))
        {   SqlCommand cmd = new SqlCommand("spBudget", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@budget1",b1);
            cmd.Parameters.AddWithValue("@budget2",b2);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            Response.Write(float.Parse(Request["budget1"].ToString()));

            XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", null));
            XElement nodecentre = new XElement("Centre_Commercial");
            document.Add(nodecentre);
            while (dr.Read())
            {
                nodecentre.Add(new XElement("Num_Centre", Convert.ToInt32(dr[0].ToString())));
                nodecentre.Add(new XElement("Nom_Centre", dr[1].ToString()));
                nodecentre.Add(new XElement("Date_debut_realisation", Convert.ToDateTime(dr[2].ToString()).Date));
                nodecentre.Add(new XElement("budget", int.Parse(dr[3].ToString())));
                nodecentre.Add(new XElement("Duree_realisation", int.Parse(dr[4].ToString())));
                nodecentre.Add(new XElement("Date_lancement", Convert.ToDateTime(dr[5].ToString())));
                nodecentre.Add(new XElement("adresse", dr[6].ToString()));
                nodecentre.Add(new XElement("ville", dr[7].ToString()));
                nodecentre.Add(new XElement("Nombre_etage", int.Parse(dr[8].ToString())));
             }
            con.Close();
            document.Save(Server.MapPath(@"~/App_Data/test.Xml"));

        }

提前致谢

【讨论】:

  • 存储过程调用有没有可能什么都不返回?也请分享您的 SP 源代码。很可能您需要添加 SET NOCOUNT ON;作为存储过程的第一行。
  • @存储过程它工作正常我已经将结果放在 sql server 存储过程工作正常创建过程 spBudget(@budget1 int,@budget2 int) as begin select * from Centre_Commercial where budget between @b和@b结束
  • 我更新了建议的解决方案,并展示了如何修改您现有的存储过程。
  • @YitzhakKhabinsky 如果您有任何解决方案,请在 xml 中始终保留两行,提前致谢
【解决方案2】:

您需要将.Save(...) 方法调用移出循环。

c#

}
con.Close();
document.Save(Server.MapPath(@"~/App_Data/test.Xml"));

SQL

您很可能需要将SET NOCOUNT ON; 添加为存储过程的第一行。

create procedure spBudget
(
    @budget1 int,
    @budget2 int
) as 
begin 
    SET NOCOUNT ON;
    select * from Centre_Commercial where budget between @b and @b;
end

【讨论】:

    【解决方案3】:

    这是另一种方法。

    MS SQL Server 将根据所需的输出生成/塑造 XML。它是带有附加 FOR XML 子句的常规 SELECT ... 语句。我正在使用动态 SQL,因为我没有您的数据库,而且我不知道您想要的输出 XML。

    (1) 您只需要稍微修改SqlCommand 变量以适应您的带有参数的存储过程。 (2) 您的存储过程需要添加FOR XML 子句。

    c#

    void Main()
    {
        const string FILENAME = @"e:\temp\file2.xml";
    
        using (SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=pubs;Data Source=SPACESHIP"))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "SELECT TOP(2) * FROM dbo.authors FOR XML PATH('r'), TYPE, ROOT('root')";
            con.Open();
    
            using (XmlReader reader = cmd.ExecuteXmlReader())
            {
                XDocument xdoc = XDocument.Load(reader);
    
                var settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.OmitXmlDeclaration = false;
                settings.IndentChars = "\t";
                // to remove BOM
                settings.Encoding = new UTF8Encoding(false);
    
                using (var writer = XmlWriter.Create(FILENAME, settings))
                {
                    xdoc.Save(writer);
                }
            }
            Console.WriteLine("File '{0}' has been created.", FILENAME);
        }
    }
    

    输出 XML 文件

    <?xml version="1.0" encoding="utf-8"?>
    <root>
       <r>
          <au_id>172-32-1176</au_id>
          <au_lname>White</au_lname>
          <au_fname>Johnson</au_fname>
          <phone>408 496-7223</phone>
          <address>10932 Bigge Rd.</address>
          <city>Menlo Park</city>
          <state>CA</state>
          <zip>94025</zip>
          <contract>1</contract>
       </r>
       <r>
          <au_id>213-46-8915</au_id>
          <au_lname>Green</au_lname>
          <au_fname>Marjorie</au_fname>
          <phone>415 986-7020</phone>
          <address>309 63rd St. #411</address>
          <city>Oakland</city>
          <state>CA</state>
          <zip>94618</zip>
          <contract>1</contract>
       </r>
    </root>
    

    【讨论】:

    • 我得到的函数测试只返回函数的:System.Collections.Generic.List`1[ControlWCSNew.Models.Centre_Commercial]
    • Kamilia,我给了你一个最小的可重现的例子。运行它,从中学习,并应用于您的确切场景。另外,请在 LinkedIn 上与我联系。
    猜你喜欢
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2017-05-29
    • 2020-06-10
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多