Asp.net 如何使XMLTextWriter和XMLWriterSettings协同工作
我有以下设置代码:Asp.net 如何使XMLTextWriter和XMLWriterSettings协同工作,asp.net,xml,xmlwriter,Asp.net,Xml,Xmlwriter,我有以下设置代码: Dim settings As XmlWriterSettings = New XmlWriterSettings() settings.Indent = True settings.OmitXmlDeclaration = True settings.NewLineOnAttributes = True 然后我为作者编写了以下代码: Dim xml As New XmlTextWriter(Serve
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作为新的XmlTextWriter(Server.MapPath(“output.xml”),enc)
xml已生成,但未应用设置。这无法获得
XmlTextWriter
,但老实说,在写入文件时,我始终使用XmlWriter
(XmlWriter
是XmlTextWriter
的基类)
您可以使用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>
这不会为您提供
XmlTextWriter
,但老实说,在写入文件时,我总是使用XmlWriter
(XmlWriter
是XmlTextWriter
的基类)
您可以使用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>
谢谢你的帮助,安迪,非常感谢。我现在已经按照您的建议进行了代码设置,但是当我尝试生成xml时,它返回为空。Dim xml作为XmlWriter=XmlWriter.Create(Server.MapPath(“output.xml”),设置)是否正确?谢谢你看起来不错-你能编辑你的问题并提供相关的代码示例吗?我已经在我的答案中添加了你的示例代码生成的内容,这似乎考虑到了你的设置。您是否记得在编写之后调用xml.Close()?是的,因为“finally”,您的异常没有被捕获。最后,无论是否发生异常,都会执行块,因此它会覆盖异常标签。如果删除catch块,您将看到异常-即存在“:”字符。有关决议,请参阅我编辑的答案。您的成功消息也应该进入try块,以确保如果发生异常,您不会看到该消息。设置为null,或者Server.MapPath的结果为null。请尝试对您的路径(例如Server.MapPath(“C:\output.xml”,settings))进行编码,看看这是否是罪魁祸首。感谢您的帮助,Andy,非常感谢。我现在已经按照您的建议进行了代码设置,但是当我尝试生成xml时,它返回为空。Dim xml作为XmlWriter=XmlWriter.Create(Server.MapPath(“output.xml”),设置)是否正确?谢谢你看起来不错-你能编辑你的问题并提供相关的代码示例吗?我已经在我的答案中添加了你的示例代码生成的内容,这似乎考虑到了你的设置。您是否记得在编写之后调用xml.Close()?是的,因为“finally”,您的异常没有被捕获。最后,无论是否发生异常,都会执行块,因此它会覆盖异常标签。如果删除catch块,您将看到异常-即存在“:”字符。有关决议,请参阅我编辑的答案。您的成功消息也应该进入try块,以确保如果发生异常,您不会看到该消息。设置为null,或者Server.MapPath的结果为null。请尝试对您的路径(例如,Server.MapPath(“C:\output.xml”,settings))进行编码,看看这是否是罪魁祸首。如何创建xml文档并在文本框上显示我们使用2个文本框进行输入,5个按钮用于不同目的如何创建xml文档并在文本框上显示我们使用2个文本框进行输入,5个按钮用于不同目的
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)
{
}
}