C # Add an empty node whose xml content does not wrap and whose property is xsi: nil = true

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.