This time in order to record the development of a WebService based interface, to parse the other side sent over the XML string. Some problems encountered in actual use.

The format of the transmitted XML is roughly as follows:


      
<messages xmlns="http://www.test.com/hit/rhin" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com/hit/rhin file:///e:/request_message.xsd">
    <heartbeat/>
    <switchset></switchset>
    <business>
        <datasets>
            <setdetails>
                <WS02_01>content</WS02_01>
            </setdetails>
            <setdetails>
                <WS02_01>content</WS02_01>
            </setdetails>
        </datasets>
    </business>
</messages>
Copy the code
  1. XMLNS is the default namespace. In the case of namespaces, XmlNamespaceManager is used for processing.
  2. XMLNS :xsi defines a namespace prefix xsi corresponding to a unique string www.w3.org/2001/XMLSch… And this XMLNS: XSI seems to be present in different XML documents. This is because XSI has become the industry default namespace for XSD (XML Schema Definition) files. XSD files (also known as Schema files) are used to define the structure of XML documents.

The XML parser can parse one XSD file from the contents of another XML file to determine whether the structure of the file is consistent with that defined in the XSD file. XSD files can be thought of as syntax or format checkers that XML documents can customize.

  1. Xsi :schemaLocation is a key-value pair separated by Spaces. The preceding key www.test.com/hit/rhin is a namespace and is a globally unique string. The latter value is the XSD location URI, which indicates the location of the XSD file corresponding to the previous namespace. XML Parser can use this information to get the XSD file. All element structures belonging to the namespace www.test.com/hit/rhin are verified through an XSD file, so the value must be accessible and accessed from the contents of an XSD file

Specific implementation code:

XmlDocument XML = new XmlDocument(); xml.LoadXml(XMLData); // Namespace handling XmlNamespaceManager m = new XmlNamespaceManager(xml.nametable); m.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); m.AddNamespace("d", "http://www.test.com/hit/rhin"); //d is an arbitrary name // read data node, Need to bring a namespace m XmlNodeList dataNodeList = XML. SelectNodes (" d: messages/d: business/d: datasets/d: setdetails ", m); if (dataNodeList ! = null && dataNodeList.Count > 0) { for (int i = 0; i < dataNodeList.Count; i++) { var model = XmlToEntity<TestModel>(dataNodeList[i].OuterXml,"http://www.test.com/hit/rhin"); }}Copy the code

The XmlToEntity method deserializes the XML onto the Model entity.

privated T XmlToEntity<T>(string xml, string nameSpace) { T obj = default(T); try { XmlRootAttribute root = new XmlRootAttribute("setdetails"); root.Namespace = nameSpace; // Operation with namespace XmlSerializer serializer = new XmlSerializer(typeof(T),root); StringReader sr = new StringReader(xml); obj = (T)serializer.Deserialize(sr); sr.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } return obj; }Copy the code

Entity definitions also need to specify namespaces

[XmlRoot(Namespace = "http://www.test.com/hit/rhin", IsNullable = false, ElementName = "setdetails")] [Table("TAB_TEST")] public class TestModel { [XmlElement("WS02_01")] public string NEIRONG { get; set; }}Copy the code