2010-03-10 7 views
0

je ce code pour les paramètres:Comment faire XmlTextWriter et XmlWriterSettings travailler ensemble

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

Ensuite, je le code pour l'écrivain:

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

S'il vous plaît pouvez-vous me dire comment je fais les paramètres s'appliquent à l'écrivain? Merci beaucoup, Phil.

EDIT: Code échantillon

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 

Il fonctionne très bien si je l'utilise;

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

le xml est produit, mais les paramètres ne sont pas appliqués.

Répondre

3

Cela ne vous donnera pas un XmlTextWriter, mais pour être honnête, je l'ai toujours utilisé XmlWriter lors de l'écriture dans un fichier de toute façon (XmlWriter est la classe de base de XmlTextWriter.)

Vous pouvez utiliser XmlWriter.Create(Server.MapPath("output.xml"), settings) qui donnera vous un XmlWriter au lieu d'un XmlTextWriter. Votre encodage devra alors être définie dans vos paramètres par exemple

EDIT (settings.Encoding = enc.):

Le code fourni pour me produit:

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

EDIT 2:

Votre espace de noms cause un problème parce qu'il essaie de mettre le nom de l'élément comme ns1:securityid quand il devrait être le nom de l'élément est securityid et l'espace de noms ns1. Vous aurez besoin de les séparer comme vous l'avez fait dans l'appel WriteAttributeString, comme ceci:

au lieu de: xml.WriteStartElement("ns1:biographicalcaptureElement") utilisation: xml.WriteStartElement("biographicalcaptureElement", "ns1")

Avec ces changements en place, je reçois maintenant:

<!----> 
<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> 
+0

Merci pour l'aide Andy, très apprécié. J'ai maintenant la configuration de code comme vous le suggérez, mais quand j'essaye de générer le xml il revient vide. Dim xml As XmlWriter = XmlWriter.Create (Server.MapPath ("output.xml"), paramètres) Est-ce correct? Merci – Phil

+0

Ouais cela semble bien - pouvez-vous éditer votre question et fournir l'exemple de code pertinent? –

+0

Merci, ont ajouté l'exemple de code. – Phil

0
public partial class XMLWriter : System.Web.UI.Page 
{ 
    static string [email protected]"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) 
    { 

    } 

} 
+1

comment créer document xml et afficher sur la zone de texte, nous prenons 2 zone de texte pour la saisie et cinq bouton pour un but différent –

Questions connexes