本文整理汇总了C#中System.Xml.XmlDocument.WriteContentTo方法的典型用法代码示例。如果您正苦于以下问题:C# XmlDocument.WriteContentTo方法的具体用法?C# XmlDocument.WriteContentTo怎么用?C# XmlDocument.WriteContentTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.XmlDocument
的用法示例。
在下文中一共展示了XmlDocument.WriteContentTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public bool Execute()
{
if (!File.Exists(WebConfig))
return false;
var fileContents = new XmlDocument();
fileContents.Load(WebConfig);
var nodes = fileContents.SelectNodes("/configuration/system.serviceModel/client/endpoint");
if (nodes == null || nodes.Count == 0)
return false;
foreach(var node in nodes)
{
var address = (node as XmlNode).Attributes["address"].Value;
var splitAddress = address.Split('/');
var newAddress = Url + "/" + address[address.Length - 1];
(node as XmlNode).Attributes["address"].Value = newAddress;
}
using (var writer = XmlWriter.Create(WebConfig))
{
fileContents.WriteContentTo(writer);
}
return true;
}
示例2: FormatXml
public static String FormatXml(String value)
{
using (var mStream = new MemoryStream())
{
using (var writer = new XmlTextWriter(mStream, Encoding.Unicode) { Formatting = System.Xml.Formatting.Indented})
{
var document = new XmlDocument();
try
{
document.LoadXml(value);
document.WriteContentTo(writer);
writer.Flush();
mStream.Flush();
mStream.Position = 0;
var sReader = new StreamReader(mStream);
return sReader.ReadToEnd();
}
catch (XmlException)
{
}
}
}
return value;
}
示例3: FormatXml
public static String FormatXml(String xml)
{
var mStream = new MemoryStream();
var document = new XmlDocument();
var writer = new XmlTextWriter(mStream, Encoding.Unicode)
{
Formatting = Formatting.Indented
};
try
{
document.LoadXml(xml);
document.WriteContentTo(writer);
writer.Flush();
mStream.Flush();
mStream.Position = 0;
var sReader = new StreamReader(mStream);
return sReader.ReadToEnd();
}
catch (XmlException)
{
return string.Empty;
}
finally
{
mStream.Close();
writer.Close();
}
}
示例4: PrepareComponentUpdateXml
private static string PrepareComponentUpdateXml(string xmlpath, IDictionary<string, string> paths)
{
string xml = File.ReadAllText(xmlpath);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("avm", "avm");
manager.AddNamespace("cad", "cad");
XPathNavigator navigator = doc.CreateNavigator();
var resourceDependencies = navigator.Select("/avm:Component/avm:ResourceDependency", manager).Cast<XPathNavigator>()
.Concat(navigator.Select("/avm:Component/ResourceDependency", manager).Cast<XPathNavigator>());
foreach (XPathNavigator node in resourceDependencies)
{
string path = node.GetAttribute("Path", "avm");
if (String.IsNullOrWhiteSpace(path))
{
path = node.GetAttribute("Path", "");
}
string newpath;
if (paths.TryGetValue(node.GetAttribute("Name", ""), out newpath))
{
node.MoveToAttribute("Path", "");
node.SetValue(newpath);
}
}
StringBuilder sb = new StringBuilder();
XmlTextWriter w = new XmlTextWriter(new StringWriter(sb));
doc.WriteContentTo(w);
w.Flush();
return sb.ToString();
}
示例5: SaveProfile
public static bool SaveProfile(XmlDocument xml, string path)
{
XmlTextWriter textWriter = null;
FileStream fs = null;
try
{
fs = new FileStream(path, FileMode.OpenOrCreate);
textWriter = new XmlTextWriter(fs, Encoding.UTF8);
textWriter.Formatting = Formatting.Indented;
xml.WriteContentTo(textWriter);
}
catch (IOException io)
{
//TODO : Error Log
Console.WriteLine(io.StackTrace);
return false;
}
finally
{
if (textWriter != null)
{
try
{
textWriter.Close();
}
catch (Exception)
{
}
}
}
return true;
}
示例6: XMLPrint
public static String XMLPrint(this String xml){
if (string.IsNullOrEmpty(xml))
return xml;
String result = "";
var mStream = new MemoryStream();
var writer = new XmlTextWriter(mStream, Encoding.Unicode);
var document = new XmlDocument();
try{
document.LoadXml(xml);
writer.Formatting = Formatting.Indented;
document.WriteContentTo(writer);
writer.Flush();
mStream.Flush();
mStream.Position = 0;
var sReader = new StreamReader(mStream);
String formattedXML = sReader.ReadToEnd();
result = formattedXML;
}
catch (XmlException){
}
mStream.Close();
writer.Close();
return result;
}
示例7: button2_Click
private void button2_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
//To create the XML DOM Tree
doc.Load(@"D:\MS.Net\CSharp\XMLParser\XMLParser\Emp.xml");
XmlElement emp = doc.CreateElement("Emp");
XmlElement eid = doc.CreateElement("EId");
XmlElement enm = doc.CreateElement("EName");
XmlElement bs = doc.CreateElement("Basic");
eid.InnerText = textBox1.Text;
enm.InnerText = textBox2.Text;
bs.InnerText = textBox3.Text;
emp.AppendChild(eid);
emp.AppendChild(enm);
emp.AppendChild(bs);
emp.SetAttribute("Department", textBox4.Text);
emp.SetAttribute("Designation", textBox5.Text);
doc.DocumentElement.AppendChild(emp);
//Writing to file
XmlTextWriter tr =
new XmlTextWriter(@"D:\MS.Net\CSharp\XMLParser\XMLParser\Emp.xml", null);
tr.Formatting = Formatting.Indented;
doc.WriteContentTo(tr);
tr.Close();//save the changes
}
示例8: PrettyPrintXml
void PrettyPrintXml(string xml)
{
var document = new XmlDocument();
try
{
document.LoadXml(xml);
using (var stream = new MemoryStream())
{
using (var writer = new XmlTextWriter(stream, Encoding.Unicode))
{
writer.Formatting = System.Xml.Formatting.Indented;
document.WriteContentTo(writer);
writer.Flush();
stream.Flush();
stream.Position = 0;
var reader = new StreamReader(stream);
xml = reader.ReadToEnd();
}
}
}
catch (XmlException)
{
}
}
示例9: IndentXMLString
/// <summary>
/// Auto-formats and indents an unformatted XML string.
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
private static string IndentXMLString(string xml)
{
string outXml = string.Empty;
MemoryStream ms = new MemoryStream();
// Create a XMLTextWriter that will send its output to a memory stream (file)
XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.Unicode);
XmlDocument doc = new XmlDocument();
// Load the unformatted XML text string into an instance
// of the XML Document Object Model (DOM)
doc.LoadXml(xml);
// Set the formatting property of the XML Text Writer to indented
// the text writer is where the indenting will be performed
xtw.Formatting = Formatting.Indented;
// write dom xml to the xmltextwriter
doc.WriteContentTo(xtw);
// Flush the contents of the text writer
// to the memory stream, which is simply a memory file
xtw.Flush();
// set to start of the memory stream (file)
ms.Seek(0, SeekOrigin.Begin);
// create a reader to read the contents of
// the memory stream (file)
StreamReader sr = new StreamReader(ms);
// return the formatted string to caller
return sr.ReadToEnd();
}
示例10: WriteToStream
private void WriteToStream(XmlDocument document, Stream stream)
{
XmlTextWriter writer = new XmlTextWriter(stream, Encoding.GetEncoding("ISO-8859-1"));
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
document.WriteContentTo(writer);
writer.Close();
}
示例11: ReformatXmlString
private static MemoryStream ReformatXmlString(string xmlstream)
{
MemoryStream stream = new MemoryStream();
XmlTextWriter formatter = new XmlTextWriter(stream, Encoding.UTF8);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstream);
formatter.Formatting = Formatting.Indented;
doc.WriteContentTo(formatter);
formatter.Flush();
return stream;
}
示例12: SaveXmlInStream
/**
* Turning the DOM4j object in the specified output stream.
*
* @param xmlContent
* The XML document.
* @param outStream
* The Stream in which the XML document will be written.
* @return <b>true</b> if the xml is successfully written in the stream,
* else <b>false</b>.
*/
public static void SaveXmlInStream(XmlDocument xmlContent,
Stream outStream)
{
//XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlContent.NameTable);
//nsmgr.AddNamespace("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.OmitXmlDeclaration = false;
XmlWriter writer = XmlTextWriter.Create(outStream,settings);
//XmlWriter writer = new XmlTextWriter(outStream,Encoding.UTF8);
xmlContent.WriteContentTo(writer);
writer.Flush();
}
示例13: Main
static void Main(string[] args)
{
#if DEBUG
args = new string[] { "d:\\projects\\csharp\\csquery\\build\\csquery.nuspec", "d:\\projects\\csharp\\csquery\\build\\csquery.test.nuspec", "-version", "1" };
#endif
if (args.Length < 4 || args.Length % 2 != 0)
{
Console.WriteLine("Call with: ProcessNuspec input output [-param value] [-param value] ...");
Console.WriteLine("e.g. ProcessNuspec../source/project.nuspec.template ../source/project.nuspec -version 1.3.3 -id csquery");
}
string input = Path.GetFullPath(args[0]);
string output = Path.GetFullPath(args[1]);
int argPos = 2;
var dict = new Dictionary<string, string>();
while (argPos < args.Length)
{
var argName = args[argPos++];
var argValue = args[argPos++];
if (!argName.StartsWith("-")) {
throw new Exception("Every argument must be a -name/value pair.");
}
dict[argName.Substring(1)]=argValue;
}
XmlDocument xDoc = new XmlDocument();
xDoc.Load(input);
foreach (var item in dict) {
var nodes = xDoc.DocumentElement.SelectNodes("//" + item.Key );
if (nodes.Count == 1)
{
var node = nodes[0];
if (dict.ContainsKey(node.Name) && node.ChildNodes.Count==1)
{
node.ChildNodes[0].Value = item.Value;
}
}
}
//string outputText = "<?xml version=\"1.0\"?>";
XmlWriter writer = XmlWriter.Create(output);
xDoc.WriteContentTo(writer);
writer.Flush();
writer.Close();
}
示例14: LoadDocumentIntoMessage
private byte[] LoadDocumentIntoMessage(XmlDocument doc)
{
Stream ms = new MemoryStream();
XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms, _wcfBinaryDictionary);
doc.WriteContentTo(writer);
writer.Flush();
byte[] bb = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(bb, 0, (int)ms.Length);
return bb;
}
示例15: ConvertXmlToWcfBinary
public static byte[] ConvertXmlToWcfBinary(XmlDocument document)
{
byte[] binaryContent = null;
using (MemoryStream ms = new MemoryStream())
{
XmlDictionaryWriter binaryWriter = XmlDictionaryWriter.CreateBinaryWriter(ms);
document.WriteContentTo(binaryWriter);
binaryWriter.Flush();
ms.Position = 0;
int length = int.Parse(ms.Length.ToString());
binaryContent = new byte[length];
ms.Read(binaryContent, 0, length);
ms.Flush();
}
return binaryContent;
}