【问题标题】:Stripping newlines / blank spaces at the end from XML feed从 XML 提要中去除末尾的换行符/空格
【发布时间】:2013-08-02 10:02:58
【问题描述】:

我们有一个 xml 提要,我需要从字段中去除换行符并修剪结束的空白。我通过以下方式生成 XML:

Protected Sub GenerateXML(ByVal type As String)
        Dim ds As New DataSet
        Dim connStr As String = "database=M**********"
        Dim selectCommand As String
    If type = "standard" Then
        selectCommand = "select * from JobList where username = '" & username & "' and status in ('" & status & "','Cancelled', 'Assessed', 'Remove')"
    Else
        selectCommand = "select * from JobList where username = '" & username & " '"
    End If

    Using conn As New SqlConnection(connStr)
        Dim command As New SqlCommand(selectCommand, conn)
        conn.Open()
        ds.DataSetName = "Locations"
        ds.Load(command.ExecuteReader(), LoadOption.OverwriteChanges, "Location")

        For Each r As DataRow In ds.Tables("Location").Rows
            If r("status") = "Assessed" Then
                r("status") = "Cancelled"
            End If
        Next
        ds.AcceptChanges()


        Response.ContentType = "text/xml"
        ds.WriteXml(Response.OutputStream)
    End Using
End Sub

有没有办法让我通过进入 ds 的每一行来运行替换?还是我必须以另一种方式生成 XML?

谢谢

【问题讨论】:

    标签: .net xml vb.net


    【解决方案1】:

    您可以使用Response.OutputStream 创建一个XmlWriter,然后将其传递给WriteXml 这是Using the XmlWriter 上的文章。

    我对代码的猜测是:

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = false;
    settings.NewLineChars = "";
    
    
    using (XmlWriter writer = XmlWriter.Create(Response.OutputStream, settings))
    {
       ds.WriteXml(writer);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 2012-01-27
      • 2015-10-10
      • 2023-01-04
      • 1970-01-01
      相关资源
      最近更新 更多