【问题标题】:How to make XMLTextWriter and XMLWriterSettings work together如何使 XMLTextWriter 和 XMLWriterSettings 一起工作
【发布时间】:2010-03-10 07:52:26
【问题描述】:

我有这个设置代码:

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,但未应用设置。

【问题讨论】:

    标签: asp.net xml xmlwriter


    【解决方案1】:

    这不会给你一个XmlTextWriter,但老实说,我在写入文件时总是使用XmlWriterXmlWriterXmlTextWriter 的基类。)

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

    编辑:

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

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body xmlns:ns1="http://its/foo.wsdl" />
    </soap:Envelope>
    

    编辑 2:

    您的命名空间引起了问题,因为它试图将元素名称设置为 ns1:securityid,而元素名称应该是 securityid 和命名空间 ns1。您需要像在 WriteAttributeString 调用中所做的那样将它们分开,如下所示:

    而不是:xml.WriteStartElement("ns1:biographicalcaptureElement") 使用:xml.WriteStartElement("biographicalcaptureElement", "ns1")

    有了这些变化,我现在得到:

    <!---->
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body xmlns:ns1="http://its/foo.wsdl">
        <biographicalcaptureElement xmlns="ns1">
          <securityid>TEST123</securityid>
          <requestdata />
        </biographicalcaptureElement>
      </soap:Body>
    </soap:Envelope>
    

    【讨论】:

    • 感谢安迪的帮助,非常感谢。我现在按照您的建议设置了代码,但是当我尝试生成 xml 时,它返回空白。 Dim xml As XmlWriter = XmlWriter.Create(Server.MapPath("output.xml"), settings) 对吗?谢谢
    • 是的,看起来不错 - 您可以编辑您的问题并提供相关的代码示例吗?
    • 我已经在我的答案中添加了您的示例代码生成的内容,这似乎确实考虑了您的设置。写完后你还记得调用 xml.Close() 吗?
    • 对,因为“finally”,你的异常没有被捕获。无论是否发生异常,finally 块都会执行,因此它会覆盖您的异常标签。如果你删除你的 catch 块,你会看到异常 - 这是一个“:”字符的存在。请参阅我编辑的解决方案答案。您的成功消息也应该放在 try 块中,以确保如果发生异常,您不会看到该消息。
    • 要么设置为空,要么Server.MapPath的结果为空。尝试对路径进行硬编码(例如 Server.MapPath("C:\output.xml", settings)),看看这是否是罪魁祸首。
    【解决方案2】:
    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)
        {
    
        }
    
    }
    

    【讨论】:

    • 如何创建 xml 文档并显示在文本框中,我们将 2 个文本框用于输入,五个按钮用于不同目的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 2019-10-13
    • 2015-07-02
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多