I don't need to introduce how to build a new WebApi project.
Get straight to the point.
First, whatever you want to receive on the. net platform, you must get it from Request.
You must be familiar with common parameter acquisition. Here's how to get file streams from Request.
System.IO.Stream sm =Request.Content.ReadAsStreamAsync().Result
This method returns a System.IO.Stream type, and if you simply want to get the file, at this point you can save it locally.
At this time, some students may have to ask, how to read the data inside?
Here are several ways to parse xml data streams:
Convert to string type:
int len = (int)sm.Length; byte[] inputByts = new byte[len]; sm.Read(inputByts, 0, len); sm.Close(); string data = Encoding.GetEncoding("utf-8").GetString(inputByts);
2. Convert to XmlDocument (you need to first convert to string in the first way):
XmlDocument xmlDocument=new XmlDocument(); xmlDocument.LoadXml(data );
3. Turn to the target:
Method 1:
Firstly, the corresponding classes and fields are created for all nodes with sub-nodes in xml. The nodes contain nodes with sub-nodes, and the corresponding classes of the nodes are used as field types.
The following is an example:
xml data:
<?xml version="1.0" encoding="UTF-8" ?> <ws:Worker_Sync xmlns:ws="http://www.w3.org/2001/XMLSchema-instance"> <ws:Worker> <ws:Summary> <ws:ID>02741</ws:ID> <ws:Name>Tina</ws:Name> </ws:Summary> <ws:Age>true</ws:Age> </Worker> <ws:Worker> <ws:Summary> <ws:ID>02741</ws:ID> <ws:Name>Tina</ws:Name> </ws:Summary> <ws:Age>true</ws:Age> </Worker> </Worker_Sync>
According to xml, we can know that we need to create three Class es, corresponding to StudentSync, Student and Summary nodes.
After creating the class, we need to annotate it:
[System.Xml.Serialization.XmlRoot(ElementName ="Student_Sync", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] public class Student_Sync { [System.Xml.Serialization.XmlElement("Student")] public Student[] Students { get; set; } }
It may be noted here that Student uses Student [], because there are multiple Student nodes in XML.
Similar to this in Student:
[System.Xml.Serialization.XmlRoot("Student")] public class Student { [System.Xml.Serialization.XmlElement("Summary")] public Summary Summary { get; set; } [System.Xml.Serialization.XmlElement("Age")] public int Age { get; set; } }
Summary:
[System.Xml.Serialization.XmlRoot("Summary")] public class Summary { [System.Xml.Serialization.XmlElement("ID")] public string ID { get; set; } [System.Xml.Serialization.XmlElement("Name")] public string Name { get; set; } }
Once the Class is created, it can be transformed. Here we use XmlSerializer:
var serializer = new XmlSerializer(typeof(Student_Sync)); var Student_SyncItem = (Student_Sync)serializer.Deserialize(sm);
Conversion completed!
Method 2:
Use Linq for conversion (this way is not very useful, just paste the code directly, it doesn't matter with the above!) :
////// Linq mode loading /// /// /// private void Button_Click_3(object sender, RoutedEventArgs e) { //ClassDemo will not be found as the root node, so a Config root node is added. List _demo = (from item in XElement.Load("XmlDemo2.xml").Elements("ClassDemo") select new ClassDemo() { Name = item.Element("Name").Value, Members = (from mem in item.Element("Members").Elements() select mem.Value).ToList(), Children = (from child in item.Element("Children").Elements() select new ClassChild() { ID = child.Element("ID").Value, Name = child.Element("Name").Value }).ToList() } ).ToList(); MessageBox.Show("Loading success!"); }
I would be very honored if this article could help you.~
Thank you for every lovely collection site@@
Reproduced please state the source:
The first address of this article is: https://www.fhcollege.com/FHCollege/Single?FHS_Post_Id=880a0f91-104c-4bf9-b41e-adc47894ae2a
Address: https://www.fhcollege.com