C# 使用RestSharp在MySql数据库中发布
我制作了一个移动应用程序,我将使用web API将其与我的数据库连接起来。我有一个URL,我想从中发出post请求。将获取的文件是XML。对于这个请求,我使用这个XML中的rest-sharp,我不希望它包含所有的信息,而是相同的信息。这是XML文件的一部分C# 使用RestSharp在MySql数据库中发布,c#,.net,xml,xamarin.forms,restsharp,C#,.net,Xml,Xamarin.forms,Restsharp,我制作了一个移动应用程序,我将使用web API将其与我的数据库连接起来。我有一个URL,我想从中发出post请求。将获取的文件是XML。对于这个请求,我使用这个XML中的rest-sharp,我不希望它包含所有的信息,而是相同的信息。这是XML文件的一部分 <pricelist> <priceentry> <station>2</station> <name>name</name> <addr
<pricelist>
<priceentry>
<station>2</station>
<name>name</name>
<address>
<fulladdress>adreess</fulladdress>
<zipcode>10442</zipcode>
<dd>
<code>A1010400</code>
<dd_descr>adreess</dd_descr>
<dimos_descr>state</dimos_descr>
<nomos_descr>state1</nomos_descr>
</dd>
</address>
<product>
<code>12</code>
<description>Shell </description>
</product>
<price>1.568</price>
<timestamp>1574432971030</timestamp>
<company>
<code>2</code>
<name>SHELL</name>
</company>
</priceentry>
<priceentry>
<station>2</station>
<name>name</name>
<address>
<fulladdress>adreess</fulladdress>
<zipcode>10442</zipcode>
<dd>
<code>A1010400</code>
<dd_descr>adreess</dd_descr>
<dimos_descr>state</dimos_descr>
<nomos_descr>state1</nomos_descr>
</dd>
</address>
<product>
<code>12</code>
<description>Shell 95 V-Power</description>
</product>
<price>1.678</price>
<timestamp>1574432985827</timestamp>
<company>
<code>2</code>
<name>SHELL</name>
</company>
</priceentry>
<priceentry>
<station>2</station>
<name>name</name>
<address>
<fulladdress>adreess</fulladdress>
<zipcode>10442</zipcode>
<dd>
<code>A1010400</code>
<dd_descr>adreess</dd_descr>
<dimos_descr>state</dimos_descr>
<nomos_descr>state1</nomos_descr>
</dd>
</address>
<product>
<code>13</code>
<description>Shell 100 V-Power Racing</description>
</product>
<price>1.895</price>
<timestamp>1574400586377</timestamp>
<company>
<code>2</code>
<name>SHELL</name>
</company>
</priceentry>
从这个XML中,我只想从每个XML中获取产品代码为12的XML。我为模特儿上了这门课
public class code
{
public String Id { get; set; }
public String price { get; set; }
public String Name { get; set; }
public String description { get; set; }
public String lastUpdate { get; set; }
}
这是为了提出一个post请求
public void Post([FromBody]string value)
{
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnnectionString = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
conn = new MySql.Data.MySqlClient.MySqlConnection();
var client = new RestClient("http://www.fuelprices.gr/test/xml/get_prices.view");
var request = new RestRequest("API/storage", Method.POST);
request.AddParameter("Content-Type", "application/xml");
request.RequestFormat = DataFormat.Xml;
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Content-Length", "0");
request.AddHeader("Accept-Encoding", "gzip, deflate");
request.AddHeader("Host", "www.fuelprices.gr");
request.AddHeader("Cache-Control", "no-cache");
XmlSerializer serializer = new XmlSerializer(typeof(PetraileoPersistence));
PetraileoPersistence po = new PetraileoPersistence();
request.AddParameter("code", "value");
request.AddParameter("nomos_descr", "value2");
}
但是我很难分离必要的值,以便将它们插入数据库。那么,是否有任何教程,以使我想要或任何建议,将帮助我
尝试以下xml linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string URL = "http://www.fuelprices.gr/test/xml/get_prices.view";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(URL);
List<Code> codes = doc.Descendants("priceentry").Select(x => new Code()
{
id = (int)x.Element("product").Element("code"),
price = decimal.Parse(((string)x.Element("price")).Replace(",","")),
name = (string)x.Element("name"),
description = (string)x.Element("product").Element("description"),
lastUpdate = (string)x.Element("timestamp")
}).ToList();
Dictionary<int, List<Code>> dict = codes.GroupBy(x => x.id, y => y)
.ToDictionary(x => x.Key, y => y.ToList());
}
}
public class Code
{
public int id { get; set; }
public decimal price { get; set; }
public String name { get; set; }
public String description { get; set; }
public String lastUpdate { get; set; }
}
}