我正在使用xmldocument写出一个xml字符串,当元素为空时,它被写为< Element />。我希望它写< Element>< / Element>相反,我怎样才能做到这一点? [英] I am writing out an xml string using xmldocument and at the moment when an element is empty it is written out as <Element />. I would like it to write <Element></Element> instead, how can I achieve this?
问题描述
我正在使用xmldocument写出一个xml字符串,当元素为空时,它被写为< Element />。我希望它写< Element>< / Element>相反,我怎样才能做到这一点?
< font face = Calibri>
你可以使用XmlTextWriter的WriteFullEndElement方法使用<>< />关闭XML元素当元素为emply时输出。在下面的示例中,您可以看到第一个Person元素为空,结果为:< Person> < /人>而不是< Person />
代码段:
私人 void OnXmlTextWriterText()
< span style ="font-size:9pt; font-family:新宋体"> {
string filePath = @" C:/writerTest.xml" ;
XmlTextWriter writer = new XmlTextWriter (filePath,编码 .UTF8);
< p style ="margin:0in 0in 0pt"> //一些重要属性
writer.Formatting = 格式跨度> .Indented; // format
writer.Indentation = 2; //仅设置格式属性,此属性有效
writer.WriteStartDocument( true );
writer.WriteStartElement(" People" );
// attribute1
writer.WriteStartAttribute(" id" );
writer.WriteString(" 001" ; );
writer.WriteEndAttribute();
< span style ="color:green"> // attribute2
writer.WriteStartAttribute(" db" ," name" ,< span style ="color:#a31515">" http://www.google.com" );
writer.WriteString(" luccyfamily" );
writer.WriteEndAttribute();
< p style ="margin:0in 0in 0pt"> // attribute3
writer.WriteAttributeString(" department" ," development" );
// person1使用WriteStartElement
writer.WriteStartElement(" Person" ; );
// writer.WriteString(" John");
// writer.WriteEndElement();
writer.WriteFullEndElement();
// peson2使用WriteElementString
writer.WriteElementString(" Person" ," Jim" );
writer.WriteFullEndElement();
writer.WriteEndDocument();
< span style ="color:green"> // close
writer.Flush();
writer.Close();
// writer.WriteComment(" this is comment");
}
希望这会有所帮助。
谢谢。
跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度>
I am writing out an xml string using xmldocument and at the moment when an element is empty it is written out as <Element />. I would like it to write <Element></Element> instead, how can I achieve this?
Hi,
You can use the WriteFullEndElement method of XmlTextWriter to close the XML element using <></> output when the element is emply. In the following example, you can see that the first Person element is empty, and the result is: <Person> </Person> instead of <Person/>
Code snippet:
private void OnXmlTextWriterText()
{
string filePath = @"C:/writerTest.xml";
XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
//some important properties
writer.Formatting = Formatting.Indented; //format
writer.Indentation = 2; //only set formatting property, this property works
writer.WriteStartDocument(true);
writer.WriteStartElement("People");
//attribute1
writer.WriteStartAttribute("id");
writer.WriteString("001");
writer.WriteEndAttribute();
//attribute2
writer.WriteStartAttribute("db", "name", "http://www.google.com");
writer.WriteString("luccyfamily");
writer.WriteEndAttribute();
//attribute3
writer.WriteAttributeString("department", "development");
//person1 use WriteStartElement
writer.WriteStartElement("Person");
//writer.WriteString("John");
//writer.WriteEndElement();
writer.WriteFullEndElement();
//peson2 use WriteElementString
writer.WriteElementString("Person","Jim");
writer.WriteFullEndElement();
writer.WriteEndDocument();
//close
writer.Flush();
writer.Close();
//writer.WriteComment("this is comment");
}
Hope this helps.
Thanks.
这篇关于我正在使用xmldocument写出一个xml字符串,当元素为空时,它被写为< Element />。我希望它写< Element>< / Element>相反,我怎样才能做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!