最近在做一项目,对方发布一webservice接口,由本人调用接口并推送数据。要求保存数据是以接口参数的形式传输数据,现在的问题是:本人要通过xml写数据并保存XML文件。本人对这方面基本不懂,求高手们指点,最好能有示例代码,或是好点的教程。谢谢!
解决方案
10
给你点思路,本人动手试试,数据应该来自数据库吧,先查到数据,然后创建XmlDocument对象去操作,看你是需要将数据已XML文件的方式去推送数据,还是XML字符串,个人推荐字符串吧。去看看XMLDocument,或Linq to xml吧。
5
假定你的xml文件是这样的:
<?xml version=”1.0″ encoding=”gb2312″?>
<AllSetting>
<音色>0</音色>
<领域>10</领域>
</AllSetting>
<?xml version=”1.0″ encoding=”gb2312″?>
<AllSetting>
<音色>0</音色>
<领域>10</领域>
</AllSetting>
读取: XmlDocument xmldoc = new XmlDocument(); ; xmldoc.Load(Application.StartupPath + "\" + "Setting.xml"); XmlNodeList allnodes = xmldoc.SelectSingleNode("AllSetting").ChildNodes; int[] _tmp = new int[2]; int _temp = 0; foreach (XmlNode node in allnodes) { XmlElement node_to_xe = (XmlElement)node;//将子节点类型转换为XmlElement类型 if (node_to_xe.Name == "音色")//出错则设置为默认值0。范围:0-1 { _tmp[0] = int.TryParse(node_to_xe.InnerText, out _temp) == true ? _temp : 0; if (_tmp[0] < 0 || _tmp[0] > 1) { _tmp[0] = 0; } } if (node_to_xe.Name == "领域")//范围:0-16 { _tmp[1] = int.TryParse(node_to_xe.InnerText, out _temp) == true ? _temp : 0; if (_tmp[1] < 0 || _tmp[1] > 16) { _tmp[1] = 0; } } } 写入: XmlDocument xmldoc = new XmlDocument(); ; xmldoc.Load(Application.StartupPath + "\" + "Setting.xml"); XmlNodeList allnodes = xmldoc.SelectSingleNode("AllSetting").ChildNodes; foreach (XmlNode node in allnodes) { XmlElement node_to_xe = (XmlElement)node;//将子节点类型转换为XmlElement类型 if (node_to_xe.Name == "音色") { node_to_xe.InnerText = "1"; } if (node_to_xe.Name == "领域") { node_to_xe.InnerText = "2"; } } xmldoc.Save(Application.StartupPath + "\" + "Setting.xml");//保存。
10
你产生对方需要的格式,你把XML当字串处理,把你程序产生的数据提交到XML中,数据就提交了。
然后把产生的XML字串保存下来即可!
然后把产生的XML字串保存下来即可!
15
根据开发文档里面的参数来!设计成类.然后通过
XmlDocument XmlDoc = JsonConvert.DeserializeXmlNode(JsonConvert.SerializeObject(v));
来直接转换。
详细代码:
IList<Test> Tt = new List<Test>();
for (int y = 0; y < 1; y++)
{
IList<TestInfo> Ti = new List<TestInfo>();
Test json = new Test();
json.OMSOrderNo = “CGR2016053000000018”;
json.WMSOrderNo = “1111”;
json.OrderType = “PO”;
json.CustomerID = “3500001001”;
json.Status = “40”;
for (int i = 0; i < 1; i++)
{
TestInfo info = new TestInfo();
info.LineNo = “0”;
info.CustomerID = “3500001001”;
info.SKU = “671432246051333”;
info.LineStatus = “40”;
info.ExpectedQty = “1”;
info.ReceivedQty = “1”;
Ti.Add(info);
}
json.item = Ti;
Tt.Add(json);
}
JsonTest jt = new JsonTest();
jt.orderinfo = Tt;
var v = new Push<IList<JsonTest>>();
v.data = new List<JsonTest>();
v.data.Add(jt);
XmlDocument XmlDoc = JsonConvert.DeserializeXmlNode(JsonConvert.SerializeObject(v));
string s = XmlDoc.InnerXml;
public class JsonTest
{
public IList<Test> orderinfo { get; set; }
}
public class Push<T>
{
public Push()
{ }
public T data { get; set; }
}
public class Test
{
public string OMSOrderNo { get; set; }
public string WMSOrderNo { get; set; }
public string OrderType { get; set; }
public string CustomerID { get; set; }
public string Status { get; set; }
public IList<TestInfo> item { get; set; }
}
public class TestInfo
{
public string LineNo { get; set; }
public string CustomerID { get; set; }
public string SKU { get; set; }
public string LineStatus { get; set; }
public string ExpectedQty { get; set; }
public string ReceivedQty { get; set; }
}
}
XmlDocument XmlDoc = JsonConvert.DeserializeXmlNode(JsonConvert.SerializeObject(v));
来直接转换。
详细代码:
IList<Test> Tt = new List<Test>();
for (int y = 0; y < 1; y++)
{
IList<TestInfo> Ti = new List<TestInfo>();
Test json = new Test();
json.OMSOrderNo = “CGR2016053000000018”;
json.WMSOrderNo = “1111”;
json.OrderType = “PO”;
json.CustomerID = “3500001001”;
json.Status = “40”;
for (int i = 0; i < 1; i++)
{
TestInfo info = new TestInfo();
info.LineNo = “0”;
info.CustomerID = “3500001001”;
info.SKU = “671432246051333”;
info.LineStatus = “40”;
info.ExpectedQty = “1”;
info.ReceivedQty = “1”;
Ti.Add(info);
}
json.item = Ti;
Tt.Add(json);
}
JsonTest jt = new JsonTest();
jt.orderinfo = Tt;
var v = new Push<IList<JsonTest>>();
v.data = new List<JsonTest>();
v.data.Add(jt);
XmlDocument XmlDoc = JsonConvert.DeserializeXmlNode(JsonConvert.SerializeObject(v));
string s = XmlDoc.InnerXml;
public class JsonTest
{
public IList<Test> orderinfo { get; set; }
}
public class Push<T>
{
public Push()
{ }
public T data { get; set; }
}
public class Test
{
public string OMSOrderNo { get; set; }
public string WMSOrderNo { get; set; }
public string OrderType { get; set; }
public string CustomerID { get; set; }
public string Status { get; set; }
public IList<TestInfo> item { get; set; }
}
public class TestInfo
{
public string LineNo { get; set; }
public string CustomerID { get; set; }
public string SKU { get; set; }
public string LineStatus { get; set; }
public string ExpectedQty { get; set; }
public string ReceivedQty { get; set; }
}
}