conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestLang;Integrated Security=True");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT * FROM Employee; SELECT * FROM Tbl_Translate", conn);
adapter.TableMappings.Add("Table", " ");
adapter.TableMappings.Add("Table1", " ");
conn.Open();
adapter.Fill(ds);
conn.Close();
ds.DataSetName = "TOURLIST";
ds.WriteXml(@"C:\Te\Xml1.xml");
richTextBox1.Text = File.ReadAllText(@"C:\Te\Xml1.xml");
此代码生成此文件
<?xml version="1.0" standalone="yes"?>
<TOURLIST>
<ID>1</ID>
<FirstName> mohammad </FirstName>
<LastName>neshasteh</LastName>
<Designation>no</Designation>
<ID>2</ID>
<FirstName>ali</FirstName>
<LastName />
<Designation>Has</Designation>
<ID>3</ID>
<FirstName>ace</FirstName>
<LastName>eca</LastName>
<Designation />
</TOURLIST>
但我想导出这种格式
<?xml version="1.0" standalone="yes"?>
<TOURLIST>
<ID>1</ID>
<FirstName>mohammad</FirstName>
<LastName>neshasteh</LastName>
<Designation>no</Designation>
<ID>2</ID>
<FirstName>ali</FirstName>
<LastName />
<Designation>Has</Designation>
<ID>3</ID>
<FirstName>ace</FirstName>
<LastName>eca</LastName>
<Designation />
</TOURLIST>
答案 0 :(得分:0)
也许它不是最好的方法,但你可以遍历你的xml并构建字符串。
public static string GetFormat(string xmlString)
{
var xml = XDocument.Parse(xmlString);
var builder = new StringBuilder();
BuildString(xml, builder);
return builder.ToString();
}
public static void BuildString(XContainer container, StringBuilder builder)
{
var element = container as XElement;
if (element != null && !element.HasElements && !string.IsNullOrEmpty(element.Value))
{
builder.Append(element.Value + " ");
}
foreach (var xElement in container.Elements())
{
BuildString(xElement, builder);
}
}
答案 1 :(得分:0)
你的数据集中可能有2个表吗?
所以,而不是:
ds.WriteXml(@"C:\Te\Xml1.xml");
尝试类似:
ds.Tables(0).WriteXml(@"C:\Te\Xml1.xml");
或:
ds.Tables("tableName").WriteXml(@"C:\Te\Xml1.xml");
希望这有帮助!
答案 2 :(得分:0)
另一种方法是在sql中生成xml,然后将结果写入xml文件。下面的代码在t-sql中:
SELECT TOP 1 *
FROM dbo.Employee
FOR XML AUTO