1. Set the content format of the generated xml to not wrap
By default, the following code is used to create and generate xml:
Copy codeThe Code is as follows: XmlDocument doc = new XmlDocument ();
// Here is the code for creating nodes, omitting ....
// Save
Doc. Save (filename );
The generated nodes have line breaks:
<UserName>
</UserName>
This will cause verification in xsd to fail. To avoid line breaks, doc. Save (filename); can be changed:Copy codeThe Code is as follows: using (XmlTextWriter xtw = new XmlTextWriter (filename, null ))
{
// None indicates that the special format is not applied, and the other Reverse enumeration value Indented indicates indentation.
Xtw. Formatting = Formatting. None;
Doc. Save (xtw );
}
2. Add an empty node whose attribute is xsi: nil = "true ". Copy codeThe Code is as follows: public static XmlElement CreateNodeWithNullAttr (XmlDocument doc, string nodeName)
{
XmlElement element = doc. CreateElement (nodeName );
XmlAttribute attr = doc. CreateAttribute ("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance ");
Attr. Value = "true ";
Element. SetAttributeNode (attr );
// Element. Attributes. Append (attr );
Return element;
}