none
Teststring as xml with declaration RRS feed

  • Question

  • Hello,
    I use vb for help me to create a easy string in C#. 
    Works well , but not with declaration, is missing in C# however.
    <?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
    My attempts were
    
    
    Public Module XmlResponse
    Public ReadOnly Property OrderData() As String
    	Get
    		Return _
    			<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
    			<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" locale="english" xsi:noNamespaceSchemaLocation="control-1.0.xsd">
    
    
    
    Public ReadOnly Property Declaration() As String
    	Get
    		Return _
    			<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>.ToString()
    	End Get
    End Property
    Public ReadOnly Property OrderData() As String
    	Get
    		Return _
    			<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
    			<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" locale="english" xsi:noNamespaceSchemaLocation="control-1.0.xsd">
    				<controlResponse material="2.0_90-00-00" order="2.000_2100001-0000" responseToRequestID="1" responsetime=
    
    				
      Public ReadOnly Property OrderData() As XDocument
            Get
                Return RequestOrderData '.ToString()   I use here a xDocument
            End Get
    Greetings Markus


    Tuesday, January 8, 2019 5:39 PM

Answers

  • Hello,

    Here is an example to include the declaration. 

    Imports System.IO
    Imports System.Xml
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Console.WriteLine(GetData())
        End Sub
        Public Function GetData() As String
            Dim data =
                    <Products>
                        <Product>
                            <ProductName>Chai</ProductName>
                        </Product>
                        <Product>
                            <ProductName>Northwoods Cranberry Sauce</ProductName>
                        </Product>
                    </Products>
    
            Dim doc = New XmlDocument()
    
            doc.LoadXml(data.ToString())
            Dim xmldecl As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
            doc.InsertBefore(xmldecl, doc.FirstChild)
    
            Return doc.OuterXml
        End Function
    End Class

    Or we could break up responsibilities

    Imports System.IO
    Imports System.Xml
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Console.WriteLine(GetData())
        End Sub
        Public Function GetData() As String
            Dim data =
                    <Products>
                        <Product>
                            <ProductName>Chai</ProductName>
                        </Product>
                        <Product>
                            <ProductName>Northwoods Cranberry Sauce</ProductName>
                        </Product>
                    </Products>
    
            Dim doc = New XmlDocument()
    
            doc.LoadXml(data.ToString())
            Declaration(doc)
    
            Return doc.OuterXml
        End Function
        Public Sub Declaration(doc As XmlDocument)
            Dim xmldecl As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
            doc.InsertBefore(xmldecl, doc.FirstChild)
        End Sub
    End Class

    Results when saved to a file

    <?xml version="1.0" encoding="utf-8"?>
    <Products>
      <Product>
        <ProductName>Chai</ProductName>
      </Product>
      <Product>
        <ProductName>Northwoods Cranberry Sauce</ProductName>
      </Product>
    </Products>

    Results using Console.WriteLine

    <?xml version="1.0" encoding="utf-8"?><Products><Product><ProductName>Chai</ProductName></Product><Product><ProductName>Northwoods Cranberry Sauce</ProductName></Product></Products>

    Then with a namespace

    Public Sub Declaration(doc As XmlDocument)
        Dim xmldecl As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
        doc.InsertBefore(xmldecl, doc.FirstChild)
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
        Dim data As String = "<Products><Product><ProductName>Chai</ProductName></Product><Product>" &
                             "<ProductName>Northwoods Cranberry Sauce</ProductName></Product></Products>"
    
        Dim prefix As String = ""
        Dim theNamespace As String = "http://www.w3.org/2001/XMLSchema-instance"
        Dim xmlDocument As New XmlDocument()
    
        Dim xElement As XElement = XElement.Parse(data)
        Dim folioNode As XmlElement = xmlDocument.CreateElement(prefix, xElement.Name.LocalName, theNamespace)
    
        Dim nodes = From node In xElement.Elements() Select node
    
        For Each item As XElement In nodes
            Dim node = xmlDocument.CreateElement(prefix, item.Name.ToString(), theNamespace)
            node.InnerText = item.Value
            folioNode.AppendChild(node)
        Next
    
        xmlDocument.AppendChild(folioNode)
        Declaration(xmlDocument)
        Console.WriteLine(xmlDocument.OuterXml)
    End Sub

    <?xml version="1.0" encoding="utf-8"?><Products xmlns="http://www.w3.org/2001/XMLSchema-instance"><Product>Chai</Product><Product>Northwoods Cranberry Sauce</Product></Products>
    <?xml version="1.0" encoding="utf-8"?>
    <Products xmlns="http://www.w3.org/2001/XMLSchema-instance">
      <Product>Chai</Product>
      <Product>Northwoods Cranberry Sauce</Product>
    </Products>



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, January 9, 2019 12:23 PM
    Moderator
  • Hello Karen,

    thanks for the feedback. I checked it. Is ok.

    How is your opinion about 

    result.Declaration.ToString() + Environment.NewLine + result.ToString()

    Can you say a small sentence for the underline?

    'What exactly does the underline in vb do?


    Greetings Markus

    Sorry for the late reply, super busy work day.

    The underline/underscore is needed for some lines of code in VB.NET where you would of course never see this in C#. See the following.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Markus Freitag Saturday, January 12, 2019 11:20 AM
    Friday, January 11, 2019 1:10 AM
    Moderator
  • The above does not looks c#. Can you show your c# version of code?


    Hello,
    please see this link.     works well
    The idea is to get an XML message in a string without any changes.
    in C# we need \ for each "  and \r\n for linefeed and carriage return.
    in VB.net not.
       I can use it 1:1 as I got it from customer.
    //in C#
    var result = XmlResponse.OrderData; 
    resTo = result.Declaration.ToString() + Environment.NewLine + result.ToString();
    'in Vb.Net
    Public ReadOnly Property OrderData() As XDocument
    	Get
    		Return _
    			<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
    			<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" locale="english" xsi:noNamespaceSchemaLocation="control-1.0.xsd">
    				<controlResponse mater
    'What exactly does the underline in vb do?	

    I found now a way with Declaration.  

    resTo = result.Declaration.ToString() + Environment.NewLine + result.ToString();

    With best regards Markus



    • Marked as answer by Markus Freitag Saturday, January 12, 2019 11:20 AM
    Wednesday, January 9, 2019 5:19 PM

All replies

  • You enclose strings in double quotes when programming in C#.

    "Dreams really do come true."

    Tuesday, January 8, 2019 6:08 PM
  • The above does not looks c#. Can you show your c# version of code?

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 9, 2019 7:16 AM
  • Hi Markus Freitag,

    Thank you for posting here.

    For your question, how does this code be related to C#? Please provide more details.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 9, 2019 9:09 AM
  • Being this is VB.NET I'm moving this to the VB.NET forum.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 9, 2019 11:52 AM
    Moderator
  • Hello,

    Here is an example to include the declaration. 

    Imports System.IO
    Imports System.Xml
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Console.WriteLine(GetData())
        End Sub
        Public Function GetData() As String
            Dim data =
                    <Products>
                        <Product>
                            <ProductName>Chai</ProductName>
                        </Product>
                        <Product>
                            <ProductName>Northwoods Cranberry Sauce</ProductName>
                        </Product>
                    </Products>
    
            Dim doc = New XmlDocument()
    
            doc.LoadXml(data.ToString())
            Dim xmldecl As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
            doc.InsertBefore(xmldecl, doc.FirstChild)
    
            Return doc.OuterXml
        End Function
    End Class

    Or we could break up responsibilities

    Imports System.IO
    Imports System.Xml
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Console.WriteLine(GetData())
        End Sub
        Public Function GetData() As String
            Dim data =
                    <Products>
                        <Product>
                            <ProductName>Chai</ProductName>
                        </Product>
                        <Product>
                            <ProductName>Northwoods Cranberry Sauce</ProductName>
                        </Product>
                    </Products>
    
            Dim doc = New XmlDocument()
    
            doc.LoadXml(data.ToString())
            Declaration(doc)
    
            Return doc.OuterXml
        End Function
        Public Sub Declaration(doc As XmlDocument)
            Dim xmldecl As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
            doc.InsertBefore(xmldecl, doc.FirstChild)
        End Sub
    End Class

    Results when saved to a file

    <?xml version="1.0" encoding="utf-8"?>
    <Products>
      <Product>
        <ProductName>Chai</ProductName>
      </Product>
      <Product>
        <ProductName>Northwoods Cranberry Sauce</ProductName>
      </Product>
    </Products>

    Results using Console.WriteLine

    <?xml version="1.0" encoding="utf-8"?><Products><Product><ProductName>Chai</ProductName></Product><Product><ProductName>Northwoods Cranberry Sauce</ProductName></Product></Products>

    Then with a namespace

    Public Sub Declaration(doc As XmlDocument)
        Dim xmldecl As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
        doc.InsertBefore(xmldecl, doc.FirstChild)
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
        Dim data As String = "<Products><Product><ProductName>Chai</ProductName></Product><Product>" &
                             "<ProductName>Northwoods Cranberry Sauce</ProductName></Product></Products>"
    
        Dim prefix As String = ""
        Dim theNamespace As String = "http://www.w3.org/2001/XMLSchema-instance"
        Dim xmlDocument As New XmlDocument()
    
        Dim xElement As XElement = XElement.Parse(data)
        Dim folioNode As XmlElement = xmlDocument.CreateElement(prefix, xElement.Name.LocalName, theNamespace)
    
        Dim nodes = From node In xElement.Elements() Select node
    
        For Each item As XElement In nodes
            Dim node = xmlDocument.CreateElement(prefix, item.Name.ToString(), theNamespace)
            node.InnerText = item.Value
            folioNode.AppendChild(node)
        Next
    
        xmlDocument.AppendChild(folioNode)
        Declaration(xmlDocument)
        Console.WriteLine(xmlDocument.OuterXml)
    End Sub

    <?xml version="1.0" encoding="utf-8"?><Products xmlns="http://www.w3.org/2001/XMLSchema-instance"><Product>Chai</Product><Product>Northwoods Cranberry Sauce</Product></Products>
    <?xml version="1.0" encoding="utf-8"?>
    <Products xmlns="http://www.w3.org/2001/XMLSchema-instance">
      <Product>Chai</Product>
      <Product>Northwoods Cranberry Sauce</Product>
    </Products>



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, January 9, 2019 12:23 PM
    Moderator
  • The above does not looks c#. Can you show your c# version of code?


    Hello,
    please see this link.     works well
    The idea is to get an XML message in a string without any changes.
    in C# we need \ for each "  and \r\n for linefeed and carriage return.
    in VB.net not.
       I can use it 1:1 as I got it from customer.
    //in C#
    var result = XmlResponse.OrderData; 
    resTo = result.Declaration.ToString() + Environment.NewLine + result.ToString();
    'in Vb.Net
    Public ReadOnly Property OrderData() As XDocument
    	Get
    		Return _
    			<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
    			<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" locale="english" xsi:noNamespaceSchemaLocation="control-1.0.xsd">
    				<controlResponse mater
    'What exactly does the underline in vb do?	

    I found now a way with Declaration.  

    resTo = result.Declaration.ToString() + Environment.NewLine + result.ToString();

    With best regards Markus



    • Marked as answer by Markus Freitag Saturday, January 12, 2019 11:20 AM
    Wednesday, January 9, 2019 5:19 PM
  • Hello Karen,

    thanks for the feedback. I checked it. Is ok.

    How is your opinion about 

    result.Declaration.ToString() + Environment.NewLine + result.ToString()

    Can you say a small sentence for the underline?

    'What exactly does the underline in vb do?


    Greetings Markus

    Thursday, January 10, 2019 5:58 PM
  • Hello Karen,

    thanks for the feedback. I checked it. Is ok.

    How is your opinion about 

    result.Declaration.ToString() + Environment.NewLine + result.ToString()

    Can you say a small sentence for the underline?

    'What exactly does the underline in vb do?


    Greetings Markus

    Sorry for the late reply, super busy work day.

    The underline/underscore is needed for some lines of code in VB.NET where you would of course never see this in C#. See the following.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Markus Freitag Saturday, January 12, 2019 11:20 AM
    Friday, January 11, 2019 1:10 AM
    Moderator