• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

VB.Net XML Question, PLEASE HELP ME.

Ok so I have attached the XML file I am working with and my Sub. I am VERY NEW to XML.

My Problem/Question:
Here is the line I need to write out to XML:
<setting id='sql_engine'>mssql</setting>

The question is: how do I write this part: id='sql_engine'

Been Trying everything I have found in the internet but to no avail.
Thanks for all help in advance

Jon
Public Sub WriteOLRXMLSettings(ByVal ServerName As String, ByVal DataBase As String, ByVal SQLUser As String, ByVal SQLPass As String)
        'path whereever crmon is -1 dir level
        Dim settings As New XmlWriterSettings()
        settings.Indent = True
        settings.NewLineOnAttributes = True
        settings.ConformanceLevel = ConformanceLevel.Document
        settings.NewLineHandling = NewLineHandling.Entitize
 
        Using writer As XmlWriter = XmlWriter.Create("C:\users\public\xmlfile.xml", settings)
 
            writer.WriteStartDocument(True)
            writer.WriteComment("Created at " & DateTime.Now.ToString("hh:mm:ss"))
            writer.WriteStartElement("settings")
            writer.WriteElementString("ServerName", ServerName)
            writer.WriteEndElement()
            writer.WriteEndDocument()
            writer.Close()
        End Using
        End
    End Sub

Open in new window

test.txt
0
CISTECH01
Asked:
CISTECH01
  • 6
  • 4
1 Solution
 
openshacCommented:
Use:
writer.WriteAttributeString("id", "sql-engine");

Open in new window

0
 
CISTECH01Author Commented:
So where/how do I use that in the code that I posted?
0
 
CISTECH01Author Commented:
Here is what the code snippet below created:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Created at 10:35:23-->
<settings
  id="sql-engine">
  <ServerName>ServerName</ServerName>
</settings>

Not right, at least doesnt look it

            writer.WriteStartDocument(True)
            writer.WriteComment("Created at " & DateTime.Now.ToString("hh:mm:ss"))
            writer.WriteStartElement("settings")
            writer.WriteAttributeString("id", "sql-engine") 'added from your suggestion
            writer.WriteElementString("ServerName", ServerName)
            writer.WriteEndElement()
            writer.WriteEndDocument()
            writer.Close()

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
CISTECH01Author Commented:
It should come out like this


<settings>
    <setting id='sql_engine'>mssql</setting>
</settings>

Open in new window

0
 
openshacCommented:
Almost there just need to create a "setting" element

writer.WriteStartElement("settings")
writer.WriteStartElement("settings")
writer.WriteAttributeString("id", "sql-engine") 'added from your suggestion
writer.WriteElementString("ServerName", ServerName)
writer.WriteEndElement()
writer.WriteEndElement()

Open in new window

0
 
CISTECH01Author Commented:
Here is my output now:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Created at 11:28:55-->
<settings>
  <setting
    id="sql-engine">
    <setting>mssql</setting>
  </setting>
</settings>


Shouldnt the:
<setting
    id="sql-engine">
    <setting>mssql</setting>
  </setting>

be this: <setting id="sql-engine">mssql</setting>

Thats what I am trying to do, or am I still just not understanding??
Just FYI I am trying to match an XML file written by another web developer for his settings, that I will control from my app.

   Public Sub WriteOLRXMLSettings(ByVal ServerName As String, ByVal DataBase As String, ByVal SQLUser As String, ByVal SQLPass As String)
        'path whereever crmon is -1 dir level
        Try
            Dim settings As New XmlWriterSettings()
            settings.Indent = True
            settings.NewLineOnAttributes = True
            settings.ConformanceLevel = ConformanceLevel.Document
            settings.NewLineHandling = NewLineHandling.Entitize
 
            Using writer As XmlWriter = XmlWriter.Create("C:\users\public\xmlfile.xml", settings)
 
                writer.WriteStartDocument(True)
                writer.WriteComment("Created at " & DateTime.Now.ToString("hh:mm:ss"))
                writer.WriteStartElement("settings")
                writer.WriteStartElement("setting")
                writer.WriteAttributeString("id", "sql-engine")
                writer.WriteElementString("setting", "mssql")
                writer.WriteEndElement()
                writer.WriteEndElement()
                writer.WriteEndDocument()
                writer.Close()
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        End

Open in new window

0
 
openshacCommented:
Sorry, should be.

If this doesn't work try and fiddle around till you get it right, shouldn't take you long.

writer.WriteStartElement("settings")
writer.WriteElementString("ServerName", ServerName)
writer.WriteAttributeString("id", "sql-engine") 'added from your suggestion
writer.WriteEndElement()

Open in new window

0
 
CISTECH01Author Commented:
This code is what I was fiddling with when i read the last comment. You last comment causes an error in my try... catch. Bad XML form is the basic error.

The code I posted here is close but still wrong.

This just sucks, I HATE Microsoft Sometimes URGHHHHHHHHHHHH
                'writer.WriteStartElement("settings")
                'writer.WriteAttributeString("id", "sql-engine", "mssql")
                'writer.WriteEndElement()
                'writer.WriteEndDocument()

Open in new window

0
 
openshacCommented:
>>This just sucks, I HATE Microsoft Sometimes URGHHHHHHHHHHHH
Bear with me, the XmlWriter really is a good part of the framework

OK, apologies I was trying to give you the correct code off the top of my head.
Right well I knocked up a little app in C# (sorry) you'll get the idea.

The output from the below is:

<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<!--Created at 05:03:00-->
<settings>
    <setting id="sql-engine">mssql</setting>
</settings>

If you give me you exact desired output I'll tweak it so that's it's spot on for you.

writer.WriteStartDocument(true);
writer.WriteComment("Created at " + DateTime.Now.ToString("hh:mm:ss"));
writer.WriteStartElement("settings");
writer.WriteStartElement("setting" );
writer.WriteAttributeString("id", "sql-engine");
writer.WriteString("mssql");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();

Open in new window

0
 
CISTECH01Author Commented:
PERFECT!!!!!!!!!!!!!!!!
Make sense the way you have it now as well. Learned alot about the XML parser, but got alot to go.
Thanks for all your help

Jon
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now