如何将数据从数据库传递到xml? [英] How to pass data from database into xml?

查看:62
本文介绍了如何将数据从数据库传递到xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库传递数据以传递到XML节点。我正在调用的数据有两列(标题,正文),并且总是有两行或更多行。我在下面创建的方法 在客户端输出空结果。



我需要在XML代码之前使用for循环吗?如果可能的话,请进一步咨询。

 public static string createXML()
{

string xml =;
string title =;
string content =;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [########]。ConnectionString);

string commandtext =######################;
SqlCommand command = new SqlCommand(commandtext,con);
con.Open();
command.Parameters.Add(new SqlParameter(title,title));
command.Parameters.Add(new SqlParameter(Body,content));

SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
string top = reader.GetString(0);
string body = reader.GetString(1);

var result = top +< br /> + body +< br />;

XElement xeRoot = new XElement(Start);
XElement xeSendTo = new XElement(properties);
XElement xeSendTo1 = new XElement(property);
xeSendTo1.Add(new XAttribute(name,Main Content));
xeSendTo1.Add(new XAttribute(type,TEXT));
xeSendTo1.SetElementValue(value,result); // top&身体参数里面的价值。
xeSendTo.Add(xeSendTo1);
xeRoot.Add(xeSendTo);
XDocument xDoc = new XDocument(xeRoot);
xDoc.ToString();
}

返回xml;
}



任何暗示我可能出错的地方都会非常感激。感谢您的时间和帮助。

我想要的输出,如下所示:

 <  开始 >  
< 属性 >
< property name = < span class =code-attribute> type = TEXT >
< value >
标题
内容

title
content

< / value >
< / property >
< / properties >
< /开始 >

解决方案

查看你的函数createXML,没有填充返回值xml。

createXML返回一个字符串,所以你不需要使用XDocument。



while循环需要修改才能得到结果,你可以这样做:

  string  top =  ; 
string body = ;
string result = ;
while (reader.Read()){
top = reader.GetString( 0 );
body = reader.GetString( 1 );
// 检查以确保CarriageReturn LineFeed正常工作,不应使用< br />此标记
结果=结果+顶部+ \\\\ n + body + \\\\ n;
}
// 创建xml字符串
xml = < span class =code-string>
<开始>< properties>< property name ='Main'type ='TEXT'>< value> ; +结果+ < / value>< / property>< / properties>< ; /开始>

// 以满足标签和xml实体的需求在结果中它可以用cdata包装(xml解析器不应解析的字符),如此
result = <![CDATA [ + result + ]]>< /跨度>;

// 可以使用XDocument $ b $解析xml字符串b XDocument xDoc = new XDocument();
xDoc = XDocument.Parse(xml);
// 这将确保xml格式正确 - 不确定这是否是一种很好的方法,或者如果使用下面的创建XElement等方法更好。



如果你想使用XDocument,那么使用相同的while循环来获得结果和修改xml字符串的生成方式:

  //  将xml内容移到循环之外 
XElement xeRoot = new XElement( 开始);
XElement xeSendTo = new XElement( 属性);
XElement xeSendTo1 = new XElement( property );
xeSendTo1.Add( new XAttribute( name 主要内容));
xeSendTo1.Add( new XAttribute( 输入 TEXT));
xeSendTo1.SetElementValue( value,result); // top&体内参数值。
xeSendTo.Add(xeSendTo1);
xeRoot.Add(xeSendTo);
XDocument xDoc = new XDocument(xeRoot);
// 记得给xml一个值
xml = xDoc.ToString( );



您还应该考虑更改xml结构以添加标题和内容的额外元素,如下所示:

 <  开始 >  
< properties >
< property 名称 = 主要 type = TEXT >
< value >
< title > 标题< span class =code-keyword>< / title >
< 内容 > 内容< span class =code-keyword>< / content >
< / value >
< >
< title > 标题< / title >
< 内容 > 内容< / content >
< / value >
< / property >
< / properties >
< span class =code-keyword>< / St艺术 >



希望有所帮助。

我没有测试并假设您的通用代码以使数据正常工作。


string connetionString = null;

SqlConnection连接);

SqlDataAdapter适配器;

DataSet ds = new DataSet();

string sql = null;



connetionString =Data Source = servername; Initial Catalog = databsename; User ID = username; Password = password;

connection = new SqlConnection(connetionString);

sql =select *来自产品;



connection.Open();

adapter = new SqlDataAdapter(sql,connection);

adapter.Fill(ds);

connection.Close();

ds.WriteXml(Product.xml);

MessageBox.Show(完成);


谢谢大家,感谢您的帮助和回复。我使用Member 10454138的建议来创建工作解决方案,如下所示:

 SqlDataReader reader = command.ExecuteReader(); 

XElement xeRoot = new XElement( 启动);
XElement xeSendTo = new XElement( 属性);
XElement xeSendTo1 = new XElement( property );
xeSendTo1.Add( new XAttribute( name 主要内容));
xeSendTo1.Add( new XAttribute( 输入 TEXT));
xeSendTo.Add(xeSendTo1);
xeRoot.Add(xeSendTo);
XDocument xDoc = new XDocument(xeRoot);

while (reader.Read())
{
string top = reader.GetString( 0 );
string body = reader.GetString( 1 );

var result = top + < br /> + body + < br />;

string data = result.ToString();
xeSendTo1.Add( new XElement( ,( new XCData(data))));
}
return xml = xDoc.ToString();





谢谢大家,感谢您对此主题的亲切输入。感谢您的时间和帮助。 = d


I am trying to pass data from the database to pass into XML node. The data I am calling, has two columns (title, body) and will always have more than 2 or more rows. The method i have created below, is outputting a null result, on the client-side.

Would I require a for loop before the XML code? Please advice further, if possible.

public static string createXML()
       {

           string xml = "";
           string title = "";
           string content = "";

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

           string commandtext = "######################";
           SqlCommand command = new SqlCommand(commandtext, con);
           con.Open();
           command.Parameters.Add(new SqlParameter("title", title));
           command.Parameters.Add(new SqlParameter("Body", content));

           SqlDataReader reader = command.ExecuteReader();

           while (reader.Read())
           {
               string top = reader.GetString(0);
               string body = reader.GetString(1);

               var result = top + "<br />" + body + "<br />";

               XElement xeRoot = new XElement("Start");
               XElement xeSendTo = new XElement("properties");
               XElement xeSendTo1 = new XElement("property");
               xeSendTo1.Add(new XAttribute("name", "Main Content"));
               xeSendTo1.Add(new XAttribute("type", "TEXT"));
               xeSendTo1.SetElementValue("value", result);//top & body parameters inside value.
               xeSendTo.Add(xeSendTo1);
               xeRoot.Add(xeSendTo);
               XDocument xDoc = new XDocument(xeRoot);
               xDoc.ToString();
           }

           return xml;
       }


Any hints to where I may be going wrong, would be very much appreciated. Thank you for your time and help.
My desired output, as the following below:

<Start>
<properties>
<property name="Main" type="TEXT">
<value>
Title
content

title
content

</value>
</property>
</properties>
</Start>

解决方案

Looking at your function createXML, the return value xml is not being populated.
The createXML is returning a string so you do not need to use XDocument.

The while loop needs modifying to get the result only, you could do something like this:

string top = "";
string body = "";
string result = "";
while (reader.Read()){
    top = reader.GetString(0);
    body = reader.GetString(1);
    //check to make sure CarriageReturn LineFeed is working, should not use <br/> tag for this
    result = result + top + "\r\n" + body + "\r\n";
}
//create the xml string
xml = "<Start><properties><property name='Main' type='TEXT'><value>" + result + "</value></property></properties></Start>"

//To cater for tags and xml entities within the result it can be wrapped with cdata (characters the xml parser should not parse) like so
result = "<![CDATA[" + result + "]]>";

//The xml string can be parsed using the XDocument
XDocument xDoc = new XDocument();
xDoc = XDocument.Parse(xml);
//This will ensure the xml is well formed - not sure if this is a good way of doing it, or if using the creating XElement etc method below is better.


If you want to use the XDocument then use the same while loop above to get result and modify how the xml string is generated:

//Move the xml stuff outside of the loop
XElement xeRoot = new XElement("Start");
XElement xeSendTo = new XElement("properties");
XElement xeSendTo1 = new XElement("property");
xeSendTo1.Add(new XAttribute("name", "Main Content"));
xeSendTo1.Add(new XAttribute("type", "TEXT"));
xeSendTo1.SetElementValue("value", result);//top & body parameters inside value.
xeSendTo.Add(xeSendTo1);
xeRoot.Add(xeSendTo);
XDocument xDoc = new XDocument(xeRoot);
//remember to give xml a value
xml = xDoc.ToString();


You should also consider changing the xml structure to add extra elements for title and content, something like this:

<Start>
    <properties>
        <property name="Main" type="TEXT">
            <value>
                <title>Title</title>
                <content>content</content>
            </value>
            <value>
                <title>Title</title>
                <content>content</content>
            </value>
        </property>
    </properties>
</Start>


Hope that helps out.
I have not tested and assume your general code to get the data is working.


string connetionString = null;
SqlConnection connection );
SqlDataAdapter adapter ;
DataSet ds = new DataSet();
string sql = null;

connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password";
connection = new SqlConnection(connetionString);
sql = "select * from Product";

connection.Open();
adapter = new SqlDataAdapter(sql, connection);
adapter.Fill(ds);
connection.Close();
ds.WriteXml("Product.xml");
MessageBox.Show("Done");


Thank you all, for your help and reply. I used Member 10454138's suggestion to create working solution, as shown below:

SqlDataReader reader = command.ExecuteReader();

            XElement xeRoot = new XElement("start");         
            XElement xeSendTo = new XElement("properties");
            XElement xeSendTo1 = new XElement("property");
            xeSendTo1.Add(new XAttribute("name", "Main Content"));
            xeSendTo1.Add(new XAttribute("type", "TEXT"));
            xeSendTo.Add(xeSendTo1);
            xeRoot.Add(xeSendTo);
            XDocument xDoc = new XDocument(xeRoot);

            while (reader.Read())
            {
                string top = reader.GetString(0);
                string body = reader.GetString(1);

                var result = top + "<br />" + body + "<br />";

                string data = result.ToString();          
                xeSendTo1.Add(new XElement("value", (new XCData(data))));     
            }                      
            return xml = xDoc.ToString(); 



Thank you all, for your kind input into this thread. I appreciate your time and help. =D


这篇关于如何将数据从数据库传递到xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆