none
Teststring as xml RRS feed

  • Question

  • Hello,

    Is there a easier way.

     res = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" standalone=\"no\"?>\r\n"
    + "<control xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" locale=\"english\" xsi:noNamespaceSchemaLocation=\"control-1.0.xsd\">
                                                +"<controlResponse material=\"2.550_90 -7750007\" order=\"2.001-0000\" responseToRequestID=\"1\" responsetime=\"2019-01-03T14:18:57.723+01:00\" state=\"ok\">\r\n"
        + "<properties>\r\n"
         + " < orderProperties>\r\n"
           + " < orderProperty name=\"nominalValue\" state=\"ok\" value=\"1000\"/>\r\n"

    I have an xml file, I want to simulate this and put it in a string. Is there a easier and faster way? As with \"

    With best regards Markus
    Thursday, January 3, 2019 6:42 PM

Answers

  • Hello,

    I remember years ago Beth Massi mentioning the following approach but the C# purest will not care for it yet it works. Create a VB.NET class project and delete the default class.

    Next add a code module to place your xml

    Public Module XmlItem
        Public ReadOnly Property Data() As String
            Get
                Return _
                    <test>
                        <properties>
                            <orderProperties>
                                <orderProperty name="nominalValue" state="ok" value="1000"/>
                            </orderProperties>
                        </properties>
                    </test>.ToString()
            End Get
        End Property
    End Module

    Nice syntax coloring

    Add a reference to your project and call the above e.g.

    using System;
    using System.Windows.Forms;
    using ClassLibrary1; // VB.NET class project
    namespace C1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var result = XmlItem.Data;
                Console.WriteLine(result);
            }
        }
    }
    

    Results in the VS outpur window

    The beauty is zero string contatenation, the downside is there is error checking so the XML must be valid.


    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

    Thursday, January 3, 2019 7:39 PM
    Moderator
  • Hi Markus Freitag,

    According to your description, I am not sure what is the issue, could you please describe in detailed, do you want to read xml file into c# string, if so, please try the following code.

    XDocument xmldata = XDocument.Load("Test1.xml");
    string test = xmldata.ToString();

    Best regards,

    Zhanglong


    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.

    • Marked as answer by Markus Freitag Saturday, January 5, 2019 11:24 AM
    Friday, January 4, 2019 7:40 AM
    Moderator

All replies

  • Hello,

    I remember years ago Beth Massi mentioning the following approach but the C# purest will not care for it yet it works. Create a VB.NET class project and delete the default class.

    Next add a code module to place your xml

    Public Module XmlItem
        Public ReadOnly Property Data() As String
            Get
                Return _
                    <test>
                        <properties>
                            <orderProperties>
                                <orderProperty name="nominalValue" state="ok" value="1000"/>
                            </orderProperties>
                        </properties>
                    </test>.ToString()
            End Get
        End Property
    End Module

    Nice syntax coloring

    Add a reference to your project and call the above e.g.

    using System;
    using System.Windows.Forms;
    using ClassLibrary1; // VB.NET class project
    namespace C1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var result = XmlItem.Data;
                Console.WriteLine(result);
            }
        }
    }
    

    Results in the VS outpur window

    The beauty is zero string contatenation, the downside is there is error checking so the XML must be valid.


    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

    Thursday, January 3, 2019 7:39 PM
    Moderator
  • Hi Markus Freitag,

    According to your description, I am not sure what is the issue, could you please describe in detailed, do you want to read xml file into c# string, if so, please try the following code.

    XDocument xmldata = XDocument.Load("Test1.xml");
    string test = xmldata.ToString();

    Best regards,

    Zhanglong


    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.

    • Marked as answer by Markus Freitag Saturday, January 5, 2019 11:24 AM
    Friday, January 4, 2019 7:40 AM
    Moderator
  • The beauty is zero string contatenation, the downside is there is error checking so the XML must be valid.



    Hi Karen,

    Thanks a lot. You understood my question correctly, that's what I was looking for. Too bad that it is not directly in C#, but with VB is ok and clearer.

    With best regards Markus

    Saturday, January 5, 2019 11:28 AM