如何在xml中插入(C#)变量

本文关键字:变量 插入 xml | 更新日期: 2023-09-27 18:25:31

如果我说创建一个函数:

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...}

我试过在谷歌上搜索,但没有找到任何明确的答案。。。很抱歉,我对xml和c#有点陌生,那么如何在不干扰visualstudio的情况下将pName和pPrice插入xml中呢?

非常感谢你们的帮助。。。tia!

如何在xml中插入(C#)变量

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

System.Xml.LinqNamespace和XElement类是开始阅读的好地方,并且比System.Xml命名空间更容易使用

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...}

我建议您不要通过字符串串联来构建XML文档,因为它非常不可靠(如果pName包含尖括号怎么办?)。请尝试查看XElement(XLINQ)API

谷歌搜索how to add variables to a string in c#得到的第一个结果是:字符串(C#编程指南)

您可以使用+运算符来连接字符串(编译为string.Concat方法)。或者,您可以使用string.Format;有关这方面的更多信息,请参阅关于复合格式的页面。这种方法只适用于最简单的小型XML片段。

您还有几个不同类集形式的选项,这些选项将帮助您构建和使用XML,其中大部分可以在System.XML命名空间中找到。这些类将生成格式良好的XML,处理您可能忽略的小细节(例如,对某些字符的特殊处理)。不幸的是,我不知道每种方法的利弊有什么好的讨论。

我真的建议不要通过字符串串联来实现这一点。有许多不同的方法可以实现这一点,从而产生更好的结果(比如不太可能产生格式错误的XML)。

通过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();
}

使用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();
    }

使用XDocument(要求您使用.NET 3.5或更高版本):

XDocument xml = new XDocument(
                              new XElement("product",
                                                     new XElement("name", pName),
                                                     new XElement("price", pPrice)
                                          )
                             );
string xmlString = xml.ToString();

请注意,在这些方法中,只有使用XmlTextWriter的方法会流式传输,这对于非常大的XML组合可能很重要。如果您使用的是.NET 3.5或更高版本,并且不处理非常大的XML组合,我会优先选择XDocument,因为它可读性更强,使用起来更简单。