关于XmlWriter写入字符串而不是文件和xmltextwriter的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.net–XMLWritervsXMLDictionaryWriter
关于XmlWriter写入字符串而不是文件和xmltextwriter的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.net – XMLWriter vs XMLDictionaryWriter、c# – 使用xmlwriter附加xml文件、c# – 使用XmlWriter附加现有XML文件、c# – 如何使用CR生成XML,而不是XmlTextWriter中的CRLF等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- XmlWriter写入字符串而不是文件(xmltextwriter)
- .net – XMLWriter vs XMLDictionaryWriter
- c# – 使用xmlwriter附加xml文件
- c# – 使用XmlWriter附加现有XML文件
- c# – 如何使用CR生成XML,而不是XmlTextWriter中的CRLF
XmlWriter写入字符串而不是文件(xmltextwriter)
我有一个WCF服务,需要返回XML字符串。但是似乎作者只想建立一个文件,而不是一个字符串。我试过了:
string nextXMLstring = "";using (XmlWriter writer = XmlWriter.Create(nextXMLstring))
这将产生一个错误,指出nextXMLstring没有文件路径。它想要类似:
using (XmlWriter writer = XmlWriter.Create("nextXMLstring.xml"))
如何建立XML,然后将其作为字符串返回?
谢谢!!
答案1
小编典典您需要创建一个StringWriter,并将其传递给XmlWriter。
XmlWriter.Create的字符串重载用于文件名。
例如
using (var sw = new StringWriter()) { using (var xw = XmlWriter.Create(sw)) { // Build Xml with xw. } return sw.ToString();}
.net – XMLWriter vs XMLDictionaryWriter
我正在黑暗中采取一种刺激,您希望使用DataContractSerializer或通常使用de / serialization. XmlDictionaryWriter是WCF使用它进行de / serialization的基类.
从那里我将推断,在XmlDictionaryWriter中必须有一些性能调优,以使其在WCF de / serialization任务中更具性能.事实上,如果你调用WriteObject(Stream,object)而不是WriteObject(XmlWriter,object)或WriteObject(XmlDictionaryWriter,object)方法,它将为你创建一个XmlDictionaryWriter
public virtual void WriteObject(Stream stream,object graph) { CheckNull(stream,"stream"); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream,Encoding.UTF8,false); this.WriteObject(writer,graph); writer.Flush(); }
c# – 使用xmlwriter附加xml文件
//Append to xml file XmlDocument doc = new XmlDocument(); doc.Load(@"c:\\test.xml"); using (XmlWriter xmlWrite = doc.CreateNavigator().AppendChild()) { xmlWrite.WriteStartElement("image name=",Name); xmlWrite.WriteElementString("width",widthValue[1]); xmlWrite.WriteElementString("Height",heightValue[1]); xmlWrite.WriteElementString("file-size",FileSizeValue[1]); xmlWrite.WriteElementString("file-format",FileFormatValue[1]); xmlWrite.WriteElementString("resolution",ResolutionValue[1]); xmlWrite.Close(); }
这是我的示例test.xml
<job-Metadata> <slug>730s_Sales/CupWinner_0111</slug> <locations>Africa</locations> <primary-location>Africa</primary-location> <reporter>Leigh Sales</reporter> <genre>Current</genre> <copyright>CBS</copyright> <autopublish>true</autopublish> </job-Metadata>
我试图像下面的xml一样追加
<job-Metadata> <slug>730s_Sales/CupWinner_0111</slug> <locations>Africa</locations> <primary-location>Africa</primary-location> <reporter>Leigh Sales</reporter> <genre>Current</genre> <copyright>CBS</copyright> <autopublish>true</autopublish> - <image name="557684_20111101-730s_SalesCupWinner_0111_80x60.jpg"> <width>80</width> <height>60</height> <file-size>7045</file-size> <file-format>JPEG Baseline</file-format> <resolution>72</resolution> <custom-name>newsthumbnail</custom-name> </image> </job-Metadata>
提前致谢
解决方法
http://www.codeproject.com/Articles/24376/LINQ-to-XML
要么
Manipulate XML data with XPath and XmlDocument (C#)
要么
文章:How to Append to a Large XML File
我想你需要像这样将节点附加到你的xmldocuemnt
//add to elements collection doc.DocumentElement.AppendChild(node);
你需要做这样的事情
XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load("F:/Documents and Settings/Administrator/Desktop/Account.xml"); XmlElement subroot=xmlDoc.CreateElement("User"); //UserName XmlElement appendedElementUsername=xmlDoc.CreateElement("UserName"); XmlText xmlTextUserName=xmlDoc.CreateTextNode(txtUsrName.Text.Trim()); appendedElementUsername.AppendChild(xmlTextUserName); subroot.AppendChild(appendedElementUsername); xmlDoc.DocumentElement.AppendChild(subroot); //Email XmlElement appendedElementEmail=xmlDoc.CreateElement("Email"); XmlText xmlTextEmail=xmlDoc.CreateTextNode(txtEmail.Text.Trim()); appendedElementEmail.AppendChild(xmlTextEmail); subroot.AppendChild(appendedElementEmail); xmlDoc.DocumentElement.AppendChild(subroot); xmlDoc.Save("F:/Documents and Settings/Administrator/Desktop/Account.xml");if(!File.Exists("F:/Documents and Settings/Administrator/Desktop/Account.xml")) { XmlTextWriter textWritter=new XmlTextWriter("F:/Documents and Settings/Administrator/Desktop/Account.xml",null); textWritter.WriteStartDocument(); textWritter.WriteStartElement("USERS"); textWritter.WriteEndElement(); textWritter.Close(); } XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load("F:/Documents and Settings/Administrator/Desktop/Account.xml"); XmlElement subroot=xmlDoc.CreateElement("User"); //UserName XmlElement appendedElementUsername=xmlDoc.CreateElement("UserName"); XmlText xmlTextUserName=xmlDoc.CreateTextNode(txtUsrName.Text.Trim()); appendedElementUsername.AppendChild(xmlTextUserName); subroot.AppendChild(appendedElementUsername); xmlDoc.DocumentElement.AppendChild(subroot); //Email XmlElement appendedElementEmail=xmlDoc.CreateElement("Email"); XmlText xmlTextEmail=xmlDoc.CreateTextNode(txtEmail.Text.Trim()); appendedElementEmail.AppendChild(xmlTextEmail); subroot.AppendChild(appendedElementEmail); xmlDoc.DocumentElement.AppendChild(subroot); xmlDoc.Save("F:/Documents and Settings/Administrator/Desktop/Account.xml");
结果就是这样:
</USERS> <User> <UserName>Buggaya</UserName> <Email>Buggaya@gmail.com</Email> </User> </USERS>
orignal post:Append in xml document
c# – 使用XmlWriter附加现有XML文件
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Indent = true; xmlWriterSettings.NewLineOnAttributes = true; using (XmlWriter xmlWriter = XmlWriter.Create("Test.xml",xmlWriterSettings)) { xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement("School"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); xmlWriter.Close(); }
我需要动态插入节点,创建以下结构:
<?xml version="1.0" encoding="utf-8"?> <School /> <Student> <FirstName>David</FirstName> <LastName>Smith</LastName> </Student> ... <Teacher> <FirstName>David</FirstName> <LastName>Smith</LastName> </Teacher> ... </School>
我该怎么做?应从键盘读取“FirstName”和“LastName”的值,并且可以随时输入值,当然在现有值下.
解决方法
XDocument doc = XDocument.Load(xmlFilePath); XElement school = doc.Element("School"); school.Add(new XElement("Student",new XElement("FirstName","David"),new XElement("LastName","Smith"))); doc.Save(xmlFilePath);
编辑
如果要将Element添加到Existing< Student>,只需添加一个Attribute
school.add(new XElement("Student",new XAttribute("ID","ID_Value"),"Smith")));
然后,您可以向现有< Student>添加更多详细信息.通过搜索 – >得到 – >加
XElement particularStudent = doc.Element("School").Elements("Student") .Where(student => student.Attribute("ID").Value == "SearchID") .FirstOrDefault(); if(particularStudent != null) particularStudent.Add(new XElement("<NEwElementName>","<Value>");
c# – 如何使用CR生成XML,而不是XmlTextWriter中的CRLF
该文件看起来很好,验证(at wc3),并被客户接受.
但客户端供应商抱怨线路终端是CRLF,而不仅仅是CR.
好吧,我在使用C#的Win32机器上,而CRLF是Win32标准的行结束.
有没有办法改变XmlTextWriter中的行结尾?
另外 – 对于正确的XML解析器,行结尾不应该无关紧要吗?
另见:What are carriage return,linefeed,and form feed?
注意:看起来唯一的答案是横向解决方案 – 您必须使用XmlWriter而不是XmlTextWriter
解决方法
然后在SO: Writing XMLDocument to file with specific newline character (c#)引导我接受了不可接受的答案
一切都在术语…..
今天关于XmlWriter写入字符串而不是文件和xmltextwriter的分享就到这里,希望大家有所收获,若想了解更多关于.net – XMLWriter vs XMLDictionaryWriter、c# – 使用xmlwriter附加xml文件、c# – 使用XmlWriter附加现有XML文件、c# – 如何使用CR生成XML,而不是XmlTextWriter中的CRLF等相关知识,可以在本站进行查询。
本文标签: