如何使XMLTextWriter和XMLWriterSettings协同工作

时间:2010-03-10 07:52:26

标签: asp.net xml xmlwriter

我有以下设置代码:

Dim settings As XmlWriterSettings = New XmlWriterSettings()
            settings.Indent = True
            settings.OmitXmlDeclaration = True
            settings.NewLineOnAttributes = True

然后我为作者提供了这段代码:

Dim xml As New XmlTextWriter(Server.MapPath("output.xml"), enc)

请告诉我如何将设置应用于作者?

非常感谢, 菲尔。

编辑:代码示例

Sub writexml_OnClick(ByVal sender As Object, ByVal e As EventArgs)
    Try
        'Vars
        Dim securityid As String = Input_securityid.Text
        Dim enc As Encoding 

        Dim settings As XmlWriterSettings = New XmlWriterSettings()
        settings.Indent = True
        settings.OmitXmlDeclaration = True
        settings.NewLineOnAttributes = True
        settings.Encoding = enc

        'Declare the writer and set file name / settings
        Dim xml As XmlWriter = XmlWriter.Create(Server.MapPath("output.xml"), settings)

        'start document
        xml.WriteStartDocument()
        xml.WriteComment("")

        'start envelope
        xml.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")
        'start body
        xml.WriteStartElement("soap", "Body", Nothing)
        xml.WriteAttributeString("xmlns", "ns1", Nothing, "http://its/foo.wsdl")

        'start biographical capture
        xml.WriteStartElement("ns1:biographicalcaptureElement")

        'start securityid
        xml.WriteStartElement("ns1:securityid")
        xml.WriteValue(securityid)
        'end securityid 
        xml.WriteEndElement()

        'start requestdata 
        xml.WriteStartElement("ns1:requestdata")

        'end requestdata
        xml.WriteEndElement()
        'end biographical capture
        xml.WriteEndElement()

        'end body
        xml.WriteEndElement()
        'end envelope
        xml.WriteEndElement()
        'end document 
        xml.WriteEndDocument()

        'clean up
        xml.Flush()
        xml.Close()

    Catch ex As Exception
        errorlbl.Text = ex.ToString
    Finally
        errorlbl.Text = ("Created file ok")
    End Try


    End Sub

如果我使用它确实可以正常工作;

Dim xml As New XmlTextWriter(Server.MapPath("output.xml"), enc)

生成了xml,但未应用设置。

2 个答案:

答案 0 :(得分:3)

这不会让你XmlTextWriter,但老实说我总是在写文件时总是使用XmlWriterXmlWriter是{{1}的基类}}。)

您可以使用XmlTextWriter,它会为您提供XmlWriter.Create(Server.MapPath("output.xml"), settings)而不是XmlWriter。然后,您的编码将需要在设置实例中设置XmlTextWriter。)

编辑:

为我提供的示例代码产生:

settings.Encoding = enc

编辑2:

您的命名空间导致了一个问题,因为当它应该是元素名称<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body xmlns:ns1="http://its/foo.wsdl" /> </soap:Envelope> 和命名空间ns1:securityid时,它试图将元素名称设置为securityid。您需要将这些内容与ns1调用中的内容区分开来,如下所示:

代替:WriteAttributeString 使用:xml.WriteStartElement("ns1:biographicalcaptureElement")

随着这些变化,我现在得到:

xml.WriteStartElement("biographicalcaptureElement", "ns1")

答案 1 :(得分:0)

public partial class XMLWriter : System.Web.UI.Page
{
    static string strFileName=@"E:\vijay112.xml";
    static XmlTextWriter write = null;
    public static int i = 0;  

    //////       static string ProcessName=Process.GetCurrentProcess().ProcessName;
    //////static Process[] processes = Process.GetProcessesByName(ProcessName); 
    //////      if (    // {
    //////      // Application.ExitThread();
    //////      // }

    public XMLWriter()
    {

    }
    ~XMLWriter()
    {
        //write.Close(); ;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
                write = new XmlTextWriter(strFileName, null);

            div_div.InnerText = i.ToString();
        }
        catch (Exception ex)
        {
        }
    }

    public static string XMLWrite()
    {
        try
        {           

            if (i == 0)
                return "success";
            else
            {
                return "please end the"+i+"more child";
            }
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
        finally
        {
            try
            {

            }
            catch ( Exception ex){}
        }
    }

    public static void SetRootElement(string strRootElement)
    {
        write.WriteStartElement(strRootElement); 
    }
    //public static void SetAttributeString(string strRootElement)
    //{
       // write.WriteString();
    //}
    public static void SetChildElement(string strChildElement)
    {

        write.WriteStartElement(strChildElement);
    }
    public static void SetAttribute(string strAttribute, string strValue)
    {           
        write.WriteAttributeString(strAttribute, strValue);
    }
    public static void EndChildElement()
    {           
        write.WriteEndElement();

    }
    public static void EndRootElement()
    {            
        write.WriteFullEndElement();
    }

    protected void Bt_root_Click(object sender, EventArgs e)
    {
        SetRootElement(TB_key.Text);
    }
    protected void bt_child_Click(object sender, EventArgs e)
    {
        ++i;
        SetChildElement(TB_key.Text);

    }

    protected void BT_attribute_Click(object sender, EventArgs e)
    {
        SetAttribute(TB_key.Text, TB_value.Text);
    }

    protected void bt_endChild_Click(object sender, EventArgs e)
    {
                    --i; 
        EndChildElement();
    }

    protected void bt_endroot_Click(object sender, EventArgs e)
    {
        EndRootElement();
    }

    protected void bt_xml_Click(object sender, EventArgs e)
    {
        write.Close();
        XmlDocument xmldoc = new XmlDocument();
       xmldoc.Load(@"E:\vijay\SourceCodeBackEnd\PrimeroWebService\Images\vijay112.xml");
      //  write.Flush();
       Txml.Text= xmldoc.InnerXml;
    }

    protected void Txml_TextChanged(object sender, EventArgs e)
    {

    }
    protected void bt_close_Click(object sender, EventArgs e)
    {

    }

}