Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SqlDataReader未生成XML文档_C#_.net_Xml - Fatal编程技术网

C# SqlDataReader未生成XML文档

C# SqlDataReader未生成XML文档,c#,.net,xml,C#,.net,Xml,我试图为Web服务动态创建一个XML,但是当我测试该服务时,我得到了以下错误 XML分析错误:未找到任何元素 地点: 第1行第39列: --------------------------------------^ // Make a new XML document in memory. XmlDocument doc = new XmlDocument(); // Fill this document with a root element

我试图为Web服务动态创建一个XML,但是当我测试该服务时,我得到了以下错误

XML分析错误:未找到任何元素 地点: 第1行第39列: --------------------------------------^

// Make a new XML document in memory.
        XmlDocument doc = new XmlDocument();

        // Fill this document with a root element
        // named <Inventory>.
        XmlElement musicInformation = doc.CreateElement("musicInformation");

        using (SqlDataReader oDr = myCommand.ExecuteReader())
        {
            while (oDr.Read())
            {
                // Now, make a sub element named <Car> with
                // an ID attribute.
                XmlElement musicdetails = doc.CreateElement("musicdetails");
                musicdetails.SetAttribute("m_id", oDr["m_id"].ToString());

                // Build the data within the <Car> element.
                XmlElement p_id = doc.CreateElement("p_id");
                p_id.InnerText = oDr["p_id"].ToString();

                XmlElement artistname = doc.CreateElement("artistname");
                artistname.InnerText = oDr["artistname"].ToString();

                XmlElement recordname = doc.CreateElement("recordname");
                recordname.InnerText = oDr["recordname"].ToString();

                XmlElement recordtype = doc.CreateElement("recordtype");
                recordtype.InnerText = oDr["recordtype"].ToString();

                XmlElement format = doc.CreateElement("format");
                format.InnerText = oDr["format"].ToString();

                XmlElement price = doc.CreateElement("price");
                price.InnerText = oDr["price"].ToString();


                musicdetails.AppendChild(p_id);
                musicdetails.AppendChild(artistname);
                musicdetails.AppendChild(recordname);
                musicdetails.AppendChild(recordtype);
                musicdetails.AppendChild(format);
                musicdetails.AppendChild(price);

                musicInformation.AppendChild(musicdetails);
            }
            return doc;
        }
//在内存中创建新的XML文档。
XmlDocument doc=新的XmlDocument();
//用根元素填充此文档
//命名的。
xmlement musicInformation=doc.CreateElement(“musicInformation”);
使用(SqlDataReader oDr=myCommand.ExecuteReader())
{
while(oDr.Read())
{
//现在,创建名为with的子元素
//ID属性。
XmlElement musicdetails=doc.CreateElement(“musicdetails”);
SetAttribute(“m_id”,oDr[“m_id”].ToString());
//在元素中构建数据。
xmlement p_id=doc.CreateElement(“p_id”);
p_id.InnerText=oDr[“p_id”].ToString();
XmlElement artistname=doc.CreateElement(“artistname”);
artistname.InnerText=oDr[“artistname”].ToString();
XmlElement recordname=doc.CreateElement(“recordname”);
recordname.InnerText=oDr[“recordname”].ToString();
XmlElement记录类型=doc.CreateElement(“记录类型”);
recordtype.InnerText=oDr[“recordtype”].ToString();
XmlElement格式=doc.CreateElement(“格式”);
format.InnerText=oDr[“format”].ToString();
XmlElement价格=doc.CreateElement(“价格”);
price.InnerText=oDr[“price”].ToString();
音乐细节。附加儿童(p_id);
音乐细节。附录儿童(艺人姓名);
音乐细节。AppendChild(记录名);
音乐细节。追加子项(记录类型);
音乐细节.AppendChild(格式);
音乐细节。附加儿童(价格);
音乐信息。附加儿童(音乐细节);
}
退货单;
}

我想您忘了将音乐信息添加到文档中:

        }
        doc.AppendChild(musicInformation);
        return doc;
    }

作为旁注,这种将行1-to-1映射到XML元素,将列1-to-1映射到子元素的方法是
SELECT。。。对于XML AUTO,元素
。看看是否可以使用它,它可能会在C#端为您节省大量代码。