marcPerry
marcPerry

Reputation: 43

how to insert (C#) variables in xml

if i were to say create a function:

public static string createProduct(string pName, decimal pPrice)
{string postData = @"<?xml version=""1.0"" encoding=""UTF-8""?
                     <product>
                         <name>?</name>
                         <price>?</price>
                     </product>";
....some other codes...}

i have tried googling it but have not found any clear answer... and sorry i'm kinda new to xml and c#, so how do i insert pName and pPrice into xml without upsetting visual studio?

would really appreciate help from you guys...tia!

Upvotes: 3

Views: 5475

Answers (4)

JamieSee
JamieSee

Reputation: 13010

I'd really recommend not doing this by string concatenation. There are a number of different ways to accomplish this that will yield better (as in less likely to produce malformed XML) results.

Via XmlTextWriter:

string xmlString = null;

using (StringWriter xmlOutput = new StringWriter())
using(XmlTextWriter xmlWriter = new XmlTextWriter(xmlOutput))
{
    xmlWriter.WriteStartDocument();
    xmlWriter.WriteStartElement("product");
    xmlWriter.WriteElementString("name", pName);
    xmlWriter.WriteElementString("price", pPrice);
    xmlWriter.WriteEndElement();

    xmlString = xmlOutput.ToString();
}

Using XmlDocument:

    string xmlString = null;

    using (StringWriter xmlOutput = new StringWriter())
    {
        XmlDocument xmlDocument = new XmlDocument();

        XmlElement productElement = xmlDocument.CreateElement("product");

        XmlElement nameElement = xmlDocument.CreateElement("name");
        nameElement.InnerText = pName;

        XmlElement priceElement = xmlDocument.CreateElement("price");
        priceElement.InnerText = pPrice;

        productElement.AppendChild(nameElement);
        productElement.AppendChild(priceElement);
        xmlDocument.AppendChild(productElement);

        xmlDocument.Save(xmlOutput);

        xmlString = xmlOutput.ToString();
    }

Using XDocument (requires that you are using .NET 3.5 or higher):

XDocument xml = new XDocument(
                              new XElement("product",
                                                     new XElement("name", pName),
                                                     new XElement("price", pPrice)
                                          )
                             );

string xmlString = xml.ToString();

Note that of these methods only the one using XmlTextWriter will stream, which may be important for very large XML composition. If you are using .NET 3.5 or higher and are not dealing with very large XML composition, I would give preference to XDocument as it's a lot more readable and simpler to use.

Upvotes: 0

L.B
L.B

Reputation: 116098

var str = new XElement("product", 
                    new XElement("name", pName), 
                    new XElement("price", pPrice))
          .ToString();

System.Xml.Linq Namespace and XElement class are good places to start to read and easier to use than System.Xml namespace

Upvotes: 5

phoog
phoog

Reputation: 43036

A Google search for how to add variables to a string in c# yielded this page as the first result: Strings (C# Programming Guide)

You can use the + operator to concatenate strings (that compiles to the string.Concat method). Or, you could use string.Format; for more information on that, see the page on composite formatting. This approach is only suitable for the simplest of small XML fragments.

You also have several options in the form of different sets of classes that will help you build and work with XML, most of which are to be found in the System.Xml namespaces. These classes will produce well-formed XML, taking care of small details that you might overlook (for example, special handling of certain characters). Unfortunately, I don't know of a good discussion of the pros and cons of each approach.

Upvotes: 0

usr
usr

Reputation: 171178

public static string createProduct(string pName, decimal pPrice)
{string postData = @"<?xml version=""1.0"" encoding=""UTF-8""?
                     <product>
                         <name>" + pName + @"</name>
                         <price>" + pPrice+ @"</price>
                     </product>";
....some other codes...}

I recommend that you take a look at constructing the XML document not by string concatenation as it is quite unreliable (what if pName contains angle brackets?). Try looking at the XElement (XLINQ) API.

Upvotes: 2

Related Questions