从 xmlwriter 中删除保留
remove preserve from xmlwriter
我的应用程序的目标是从数据库中获取数据并将其写入桌面上的 XML 文件。
private void btnSubmit_Click(object sender, EventArgs e)
{
dtPart_Sales = new DataTable { TableName = "Part_Sales"};
Application.DoEvents();
List<string> listTemp = new List<string>(Regex.Split(textBox1.Text, Environment.NewLine));
List<string> list = listTemp.Distinct().ToList();
var joinedNames = list.Aggregate((a, b) => a + "','" + b+ "");
SqlConnection mConnection = new SqlConnection();
SqlCommand mCommand = new SqlCommand();
SqlDataAdapter mDataAdapter = new SqlDataAdapter();
mConnection.ConnectionString = Conn_Str();
mCommand.CommandType = CommandType.Text;
mCommand.Connection = mConnection;
if (chkSales.Checked)
{
try
{
mConnection.Open();
Application.DoEvents();
SqlDataAdapter da = new SqlDataAdapter("select PRTNUM_29, SLSCAT_29, PMDES1_29, PMDES2_29, STK_29, TAXABL_29, " +
"BOMUOM_29, SLSUOM_29, SLSCNV_29, PRICE_29, BREAK1_29, DISC1_29, PRICE1_29, BREAK2_29, DISC2_29, PRICE2_29," +
" BREAK3_29, DISC3_29, PRICE3_29, BREAK4_29, DISC4_29, PRICE4_29, BREAK5_29, DISC5_29, PRICE5_29, BREAK6_29," +
" DISC6_29, PRICE6_29, BREAK7_29, DISC7_29, PRICE7_29, BREAK8_29, DISC8_29, PRICE8_29, BREAK9_29, DISC9_29," +
" PRICE9_29, QTYMTD_29, SLSMTD_29, CSTMTD_29, QTYYTD_29, SLSYTD_29, CSTYTD_29, QTYLYR_29, SLSLYR_29, CSTLYR_29," +
" QTYCOM_29, CRTLTO_29, AUTOMS_29, APLDSC_29, PRDLIN_29, HISFLG_29, WARFLG_29, LABWAR_29, MATWAR_29, RETMTD_29," +
" RETYTD_29, UNWRPL_29, UNWREP_29, OUWRPL_29, OUWREP_29, COMMIS_29, TAXCDE_29, TAXCDE2_29, TAXCDE3_29, MCOMP_29," +
" MSITE_29, UDFKEY_29, UDFREF_29, ALWBCK_29, AUTOMF_29, FILLER_29, MANPRC_29, WARRES_29, CreatedBy, ModifiedBy" +
" from Part_Sales where PRTNUM_29 in ('" + joinedNames + "')", mConnection);
da.Fill(dtPart_Sales);
mConnection.Close();
string FileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\PRSBatchDL\Part_Sales" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".XML";
dtPart_Sales.WriteXml(FileName);
MessageBox.Show(FileName + " is saved!");
}
catch (Exception ex)
{
if (ex.Message == "The source contains no DataRows.")
dtPart_Sales = new DataTable();
else
{
MessageBox.Show(ex.Message.ToString());
return;
}
}
}
}
文件写出来没问题,但是。我遇到的问题是 ns1:space 被填充到某些列 headers 中,并且 preserve 被设置为数据。
我的问题是如何删除这些列?
不,在使用 WriteXml() 方法生成 xml 文件时没有直接删除 xml:space="preserve"
的方法。
有几个选择
选项 1:
加载 xml 并进行字符串替换,很简单。
XmlDocument doc = null;
doc.LoadXml(ds.GetXml())
Return doc.InnerXml.Replace(" xml:space=""preserve""", "")
选项 2:
使用 XSLT,您可以创建 XSLT 模板,然后 运行 根据它删除 xml:space 属性。需要一些 XSLT 转换知识。
选项 3:
使用 XmlReader / XmlWriter 并将它们链接起来以复制除 xml:space 属性之外的所有节点。将需要一些深度编程来处理所有可能的 xnode。
希望对您有所帮助。
我的应用程序的目标是从数据库中获取数据并将其写入桌面上的 XML 文件。
private void btnSubmit_Click(object sender, EventArgs e)
{
dtPart_Sales = new DataTable { TableName = "Part_Sales"};
Application.DoEvents();
List<string> listTemp = new List<string>(Regex.Split(textBox1.Text, Environment.NewLine));
List<string> list = listTemp.Distinct().ToList();
var joinedNames = list.Aggregate((a, b) => a + "','" + b+ "");
SqlConnection mConnection = new SqlConnection();
SqlCommand mCommand = new SqlCommand();
SqlDataAdapter mDataAdapter = new SqlDataAdapter();
mConnection.ConnectionString = Conn_Str();
mCommand.CommandType = CommandType.Text;
mCommand.Connection = mConnection;
if (chkSales.Checked)
{
try
{
mConnection.Open();
Application.DoEvents();
SqlDataAdapter da = new SqlDataAdapter("select PRTNUM_29, SLSCAT_29, PMDES1_29, PMDES2_29, STK_29, TAXABL_29, " +
"BOMUOM_29, SLSUOM_29, SLSCNV_29, PRICE_29, BREAK1_29, DISC1_29, PRICE1_29, BREAK2_29, DISC2_29, PRICE2_29," +
" BREAK3_29, DISC3_29, PRICE3_29, BREAK4_29, DISC4_29, PRICE4_29, BREAK5_29, DISC5_29, PRICE5_29, BREAK6_29," +
" DISC6_29, PRICE6_29, BREAK7_29, DISC7_29, PRICE7_29, BREAK8_29, DISC8_29, PRICE8_29, BREAK9_29, DISC9_29," +
" PRICE9_29, QTYMTD_29, SLSMTD_29, CSTMTD_29, QTYYTD_29, SLSYTD_29, CSTYTD_29, QTYLYR_29, SLSLYR_29, CSTLYR_29," +
" QTYCOM_29, CRTLTO_29, AUTOMS_29, APLDSC_29, PRDLIN_29, HISFLG_29, WARFLG_29, LABWAR_29, MATWAR_29, RETMTD_29," +
" RETYTD_29, UNWRPL_29, UNWREP_29, OUWRPL_29, OUWREP_29, COMMIS_29, TAXCDE_29, TAXCDE2_29, TAXCDE3_29, MCOMP_29," +
" MSITE_29, UDFKEY_29, UDFREF_29, ALWBCK_29, AUTOMF_29, FILLER_29, MANPRC_29, WARRES_29, CreatedBy, ModifiedBy" +
" from Part_Sales where PRTNUM_29 in ('" + joinedNames + "')", mConnection);
da.Fill(dtPart_Sales);
mConnection.Close();
string FileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\PRSBatchDL\Part_Sales" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".XML";
dtPart_Sales.WriteXml(FileName);
MessageBox.Show(FileName + " is saved!");
}
catch (Exception ex)
{
if (ex.Message == "The source contains no DataRows.")
dtPart_Sales = new DataTable();
else
{
MessageBox.Show(ex.Message.ToString());
return;
}
}
}
}
文件写出来没问题,但是。我遇到的问题是 ns1:space 被填充到某些列 headers 中,并且 preserve 被设置为数据。
我的问题是如何删除这些列?
不,在使用 WriteXml() 方法生成 xml 文件时没有直接删除 xml:space="preserve"
的方法。
有几个选择
选项 1:
加载 xml 并进行字符串替换,很简单。
XmlDocument doc = null;
doc.LoadXml(ds.GetXml())
Return doc.InnerXml.Replace(" xml:space=""preserve""", "")
选项 2:
使用 XSLT,您可以创建 XSLT 模板,然后 运行 根据它删除 xml:space 属性。需要一些 XSLT 转换知识。
选项 3:
使用 XmlReader / XmlWriter 并将它们链接起来以复制除 xml:space 属性之外的所有节点。将需要一些深度编程来处理所有可能的 xnode。
希望对您有所帮助。