2012-02-21 82 views
2

如果我是说创建一个函数:如何插入(C#)的XML变量

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

我曾尝试使用Google,但没有发现任何明确的答案...... 和抱歉,我有点新的xml和c#,所以我如何将pName和pPrice插入xml而不会影响Visual Studio?

真的很感谢你们的帮助...... tia!

+1

在问这个问题之前,你看过** String.Format **吗?你的问题显示你完全缺乏研究。在C#中构建字符串是一项非常简单的任务。 – 2012-02-21 18:18:20

+0

非常抱歉的noob问题...我会研究一下,谢谢! – marcPerry 2012-02-21 18:28:49

+0

如果您使用XML代表代码中的对象,我建议您查看XML序列化。 – walkingTarget 2012-02-21 18:47:50

回答

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

System.Xml.Linq NamespaceXElement class的好地方开始阅读,更容易比System.Xml命名空间

+1

+1非常好,很好的答案。由于OP是一个自称初学者,你可能想要解释代码究竟做了什么。 – Yuck 2012-02-21 18:28:53

1
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。

+0

这有可能导致整个XML文档格式不正确或者无法使用,具体取决于输入。我不会推荐这个解决方案; OP应该改用XML DOM。另外,你在XML中引入'@' - 你打算这么做吗? – Yuck 2012-02-21 18:21:20

+1

at标志是一个错误,谢谢。我强调了我的警告文字,因为我完全同意你的意见。然而,我认为最好总是直接给出答案。只是说“你不应该这样做”是不够的。 – usr 2012-02-21 18:25:46

0

一个谷歌搜索how to add variables to a string in c#产生此页面的第一个结果:Strings (C# Programming Guide)

可以使用+运算符连接字符串(即编译到string.Concat方法)。或者,您可以使用string.Format;欲了解更多信息,请参阅composite formatting的页面。这种方法仅适用于最简单的小型XML片段。

您还有几个选项可以帮助您构建和使用XML,其中大部分都可以在System.Xml namespaces中找到。这些类将生成格式良好的XML,处理您可能忽略的小细节(例如,特定字符的特殊处理)。不幸的是,我不清楚每种方法的优缺点。

0

我真的不建议用字符串连接这样做是为了使用。有很多不同的方法可以实现这一点,从而产生更好的结果(因为不太可能产生格式不正确的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,因为它更易读易用。