将XML转换为字符串
本文关键字:字符串 转换 XML | 更新日期: 2023-09-27 17:59:27
我目前正在处理一个项目,在该项目中,我已将值("PFOID"answers"Quantity")以XML
格式发送到存储过程。
所需的XML格式类似于以下
<string>
<PFOID>04676723-2afb-49ff-9fa1-0131cabb407c</PFOID>
<Quantity>90</Quantity>
</string>
我的代码看起来像这样。
internal void UpdateQuantity(PFO pfo)
{
string pfoIds = "<PFO>" + "<PFOID>" + pfo.PFOId.ToString() + "</PFOID>" + " " + "<Quantity>" + pfo.PlannedQty.ToString() + "</Quantity>" + "</PFO>";
//pfoIds = pfoIds.Replace("<","<").Replace(">",">");
// string pfoIds = pfo.PFOId.ToString() + pfo.PlannedQty.ToString();
//XDocument d = new XDocument(pfoIds,
// new XElement
// ("PFO",
// new XElement("PFOID", pfo.PFOId.ToString()),
// new XElement("Quantity", pfo.PlannedQty.ToString())
// )
// );
List<string> pfoIdList = new List<string>();
pfoIdList.Add(pfoIds);
XmlSerializer serializer = new XmlSerializer(pfoIdList.GetType());
StringBuilder xmlString = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Encoding = new UTF8Encoding();
XmlWriter writer = XmlWriter.Create(new StringWriter(xmlString), settings);
//XDocument xDoc = XDocument.Load(pfoIds);
//XmlNode xNode = xmlDoc.SelectSingleNode("PFOID", "Quantity");
//string onlyvalue = xNode.InnerText;
//System.Xml.Linq.XDocument xDoc = new XDocument();
////System.Xml.Linq.XNode xNode =new System.Xml.Linq.XDocument();
//XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.Load(pfoIds);
//XmlNode xNode = xmlDoc.SelectSingleNode("PFOID", "Quantity");
//string onlyvalue = xNode.InnerText;
//serializer.WriteObject(writer, SelectedStoreIds.ToArray());
serializer.Serialize(writer, pfoIdList);
if (writer != null)
writer.Close();
xmlString.ToString();
{
Context.PFOValidateUpdateData(xmlString.ToString(), WebContext.Current.User.UserID, op =>
{
IsProcessing = false;
if (ValidateUpdateCompleted != null)
ValidateUpdateCompleted(this, EventArgs.Empty);
}, null);
}
}
}
}
我尝试过使用XmlDocument
和XmlNodes
,但不幸的是silverlight system.xml程序集的库中没有这些扩展。
有人能帮我吗?我真的很乐意和你在一起。非常感谢。
看起来是这样的。。。对不起,我在<;<;PFOID>"04676723-2afb-49ff-9fa1-0131cabb407c</PFOID>;"数量"90</数量>;"
我不知道这是否是一个可能的解决方案,但你可以尝试这样做。
// Create a DataSet with one table containing two columns.
DataSet dataSet = new DataSet("dataSet");
DataTable table = dataSet.Tables.Add("string");
table.Columns.Add("PFOID", typeof(string));
table.Columns.Add("Quantity", typeof(value));
DataRow row;
row = table.NewRow();
row["PFOID"]= "04676723-2afb-49ff-9fa1-0131cabb407c";
row["Quantity"]= 90;
table.Rows.Add(row);
// Display the DataSet contents as XML.
Console.WriteLine(dataSet.GetXml());