本文整理汇总了C#中System.Xml.XmlTextWriter.WriteElementString方法的典型用法代码示例。如果您正苦于以下问题:C# XmlTextWriter.WriteElementString方法的具体用法?C# XmlTextWriter.WriteElementString怎么用?C# XmlTextWriter.WriteElementString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.XmlTextWriter
的用法示例。
在下文中一共展示了XmlTextWriter.WriteElementString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
string textFile = "../../PersonData.txt";
string xmlFile = "../../Person.xml";
using (XmlTextWriter xmlWriter = new XmlTextWriter(xmlFile, Encoding.GetEncoding("utf-8")))
{
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.IndentChar = '\t';
xmlWriter.Indentation = 1;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("person");
xmlWriter.WriteAttributeString("id", "1");
using (StreamReader fileReader = new StreamReader(textFile))
{
string name = fileReader.ReadLine();
xmlWriter.WriteElementString("name", name);
string address = fileReader.ReadLine();
xmlWriter.WriteElementString("address", address);
string phone = fileReader.ReadLine();
xmlWriter.WriteElementString("phone", phone);
}
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
}
示例2: GenerateAlbum
private static void GenerateAlbum()
{
using (var writer = new XmlTextWriter("../../../AdditionalFiles/album.xml", Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.IndentChar = '\t';
writer.Indentation = 1;
writer.WriteStartDocument();
writer.WriteStartElement("albums");
using (XmlReader reader = XmlReader.Create("../../../AdditionalFiles/catalog.xml"))
{
while (reader.Read())
{
bool isElement = reader.NodeType == XmlNodeType.Element;
if (isElement && reader.Name == "name")
{
writer.WriteStartElement("album");
string albumName = reader.ReadElementContentAsString();
writer.WriteElementString("name", albumName);
reader.ReadToFollowing("artist");
string albumAuthor = reader.ReadElementContentAsString();
writer.WriteElementString("author", albumAuthor);
writer.WriteEndElement();
}
}
}
writer.WriteEndElement();
}
}
示例3: AddAudioItem
/// <summary>
/// Add an audio track
/// </summary>
/// <param name="xmlWriter">
/// The xml writer.
/// </param>
/// <param name="audioTrack">
/// The audio track.
/// </param>
private static void AddAudioItem(XmlTextWriter xmlWriter, AudioTrack audioTrack)
{
xmlWriter.WriteStartElement("dict");
xmlWriter.WriteElementString("key", "AudioBitrate");
xmlWriter.WriteElementString("string", audioTrack.Bitrate.ToString());
xmlWriter.WriteElementString("key", "AudioEncoder");
xmlWriter.WriteElementString("string", EnumHelper<AudioEncoder>.GetDisplay(audioTrack.Encoder));
xmlWriter.WriteElementString("key", "AudioMixdown");
xmlWriter.WriteElementString("string", EnumHelper<Mixdown>.GetDisplay(audioTrack.MixDown));
xmlWriter.WriteElementString("key", "AudioSamplerate");
xmlWriter.WriteElementString("string", audioTrack.SampleRate.ToString().Replace("0", "Auto"));
xmlWriter.WriteElementString("key", "AudioTrack");
xmlWriter.WriteElementString("integer", audioTrack.Track.ToString());
xmlWriter.WriteElementString("key", "AudioTrackDRCSlider");
xmlWriter.WriteElementString("real", audioTrack.DRC.ToString());
xmlWriter.WriteElementString("key", "AudioTrackDescription");
xmlWriter.WriteElementString("string", "Unknown");
xmlWriter.WriteElementString("key", "AudioTrackGainSlider");
xmlWriter.WriteElementString("real", audioTrack.Gain.ToString());
xmlWriter.WriteEndElement();
}
示例4: ConvetTextToXml
public static void ConvetTextToXml()
{
string[] lines;
const int NumberOfTagsForEachPerson = 3;
using (var writer = new XmlTextWriter("../../toXml.xml", Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.IndentChar = '\t';
writer.Indentation = 1;
using (StreamReader reader = new StreamReader("../../fromText.txt"))
{
lines = reader.ReadToEnd().Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
writer.WriteStartDocument();
writer.WriteStartElement("Persons");
for (int i = 0; i < lines.Length; i += NumberOfTagsForEachPerson)
{
writer.WriteStartElement("Person");
writer.WriteElementString("name", lines[i]);
writer.WriteElementString("adress", lines[i + 1]);
writer.WriteElementString("telephone", lines[i + 2]);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
}
Console.WriteLine("\tDONE!!! Check file!");
}
示例5: AddRssItem
/// <summary>
/// Adds a single item to the XmlTextWriter supplied
/// </summary>
/// <param name="writer">The XmlTextWriter to be written to</param>
/// <param name="sItemTitle">The title of the RSS item</param>
/// <param name="sItemLink">The URL of the RSS item</param>
/// <param name="sItemDescription">The RSS item text itself</param>
/// <param name="sPubDate"></param>
/// <param name="bDescAsCdata">Write description as CDATA</param>
/// <returns>The XmlTextWriter with the item info written to it</returns>
public XmlTextWriter AddRssItem(XmlTextWriter writer, string sItemTitle, string sItemLink, string sItemDescription, DateTime sPubDate, bool bDescAsCdata)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", sItemTitle);
writer.WriteElementString("link", sItemLink);
if (bDescAsCdata)
{
// Now we can write the description as CDATA to support html content.
// We find this used quite often in aggregators
writer.WriteStartElement("description");
writer.WriteCData(sItemDescription);
writer.WriteEndElement();
}
else
{
writer.WriteElementString("description", sItemDescription);
}
writer.WriteElementString("pubDate", sPubDate.ToString("r"));
writer.WriteEndElement();
return writer;
}
示例6: ExportProductsToGoogleBase
/// <summary>
/// Export Products To Google Base
/// </summary>
/// <returns></returns>
public byte[] ExportProductsToGoogleBase()
{
const string ns = "http://base.google.com/ns/1.0";
var ms = new MemoryStream();
var xml = new XmlTextWriter(ms, Encoding.UTF8);
xml.WriteStartDocument();
xml.WriteStartElement("rss");
xml.WriteAttributeString("xmlns", "g", null, ns);
xml.WriteAttributeString("version", "2.0");
xml.WriteStartElement("channel");
//GENERAL FEED INFO
xml.WriteElementString("title", CurrentRequestData.CurrentSite.Name);
xml.WriteElementString("link", CurrentRequestData.CurrentSite.BaseUrl);
xml.WriteElementString("description",
" Products from " + CurrentRequestData.CurrentSite.Name + " online store");
IList<ProductVariant> productVariants = _productVariantService.GetAllVariantsForGoogleBase();
foreach (ProductVariant pv in productVariants)
{
ExportGoogleBaseProduct(ref xml, pv, ns);
}
xml.WriteEndElement();
xml.WriteEndElement();
xml.WriteEndDocument();
xml.Flush();
byte[] file = ms.ToArray();
xml.Close();
return file;
}
示例7: Serialize
public static string Serialize(UserProfileData profile)
{
StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
xtw.WriteStartElement("user_profile");
xtw.WriteAttributeString("major_version", MAJOR_VERSION.ToString());
xtw.WriteAttributeString("minor_version", MINOR_VERSION.ToString());
xtw.WriteElementString("name", profile.Name);
xtw.WriteElementString("id", profile.ID.ToString());
xtw.WriteElementString("about", profile.AboutText);
// Not sure if we're storing this yet, need to take a look
// xtw.WriteElementString("Url", profile.Url);
// or, indeed, interests
xtw.WriteEndElement();
xtw.Close();
sw.Close();
return sw.ToString();
}
示例8: GenerateDescription
private void GenerateDescription(string host = null)
{
MemoryStream memStream = new MemoryStream();
using (XmlTextWriter descWriter = new XmlTextWriter(memStream, new UTF8Encoding(false)))
{
descWriter.Formatting = Formatting.Indented;
descWriter.WriteRaw("<?xml version=\"1.0\"?>");
descWriter.WriteStartElement("root", "urn:schemas-upnp-org:device-1-0");
descWriter.WriteStartElement("specVersion");
descWriter.WriteElementString("major", "1");
descWriter.WriteElementString("minor", "0");
descWriter.WriteEndElement();
descWriter.WriteStartElement("device");
rootDevice.WriteDescription(descWriter, host);
descWriter.WriteEndElement();
descWriter.WriteEndElement();
descWriter.Flush();
descArray = memStream.ToArray();
}
}
示例9: Main
public static void Main()
{
XmlReader reader = XmlReader.Create("..\\..\\..\\XmlCatalogDirectory\\text.xml");
XmlTextWriter writer = new XmlTextWriter("..\\..\\album.xml",Encoding.UTF8);
using (writer)
{
writer.WriteStartDocument();
writer.Formatting = Formatting.Indented;
writer.IndentChar = '\t';
writer.Indentation = 1;
writer.WriteStartElement("albums");
string artist = "";
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.Element) && reader.Name == "artist")
{
artist = reader.GetAttribute("name");
}
else if ((reader.NodeType == XmlNodeType.Element) && reader.Name == "name")
{
string albumName = reader.ReadElementContentAsString();
writer.WriteStartElement("album");
writer.WriteElementString("name", albumName);
writer.WriteElementString("artist", artist);
writer.WriteEndElement();
}
}
}
}
示例10: Main
private static void Main(string[] args)
{
// 8. Write a program, which (using XmlReader and XmlWriter) reads the file catalog.xml and creates the file album.xml,
// in which stores in appropriate way the names of all albums and their authors
Encoding encoding = Encoding.UTF8;
using (XmlTextWriter writer = new XmlTextWriter(FileToWrite, encoding))
{
writer.Formatting = Formatting.Indented;
writer.IndentChar = '\t';
writer.Indentation = 1;
writer.WriteStartDocument();
writer.WriteStartElement("albums");
using (XmlTextReader reader = new XmlTextReader(FileToRead))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "name")
{
writer.WriteStartElement("album");
writer.WriteElementString("name", reader.ReadElementString());
}
else if (reader.NodeType == XmlNodeType.Element && reader.Name == "artist")
{
writer.WriteElementString("artist", reader.ReadElementString());
writer.WriteEndElement();
}
}
}
writer.WriteEndDocument();
}
}
示例11: AddXmlToRequest
/// <summary>
/// Adds the XML to web request. The XML is the standard
/// XML used by RPC-XML requests.
/// </summary>
private static void AddXmlToRequest(Uri sourceUrl, Uri targetUrl, HttpWebRequest webreqPing)
{
Stream stream = (Stream)webreqPing.GetRequestStream();
using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.ASCII))
{
writer.WriteStartDocument(true);
writer.WriteStartElement("methodCall");
writer.WriteElementString("methodName", "pingback.ping");
writer.WriteStartElement("params");
writer.WriteStartElement("param");
writer.WriteStartElement("value");
writer.WriteElementString("string", sourceUrl.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("param");
writer.WriteStartElement("value");
writer.WriteElementString("string", targetUrl.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
}
示例12: SettingsWrite
public bool SettingsWrite(string[] information)
{
XmlTextWriter xmlWrite = new XmlTextWriter(AppDomain.CurrentDomain.BaseDirectory + "\\settings.xml", System.Text.UTF8Encoding.UTF8);
xmlWrite.Formatting = Formatting.Indented;
try
{
xmlWrite.WriteStartDocument();
xmlWrite.WriteStartElement("root");
xmlWrite.WriteStartElement("settings");
xmlWrite.WriteElementString("server", information[0]);
xmlWrite.WriteElementString("cache", information[1]);
xmlWrite.WriteEndElement();
xmlWrite.WriteEndElement();
xmlWrite.Close();
DNS dns = new DNS();
dns._resolver.DnsServer = information[0];
dns._resolver.UseCache = Convert.ToBoolean(information[1]);
return true;
}
catch (Exception ex)
{
Console.WriteLine("XML Error: " + ex.Message);
return false;
}
}
示例13: CreateXmlFromTxtFile
/// <summary>
/// The create xml from txt file.
/// </summary>
/// <param name="txtFile">
/// The txt file.
/// </param>
public static void CreateXmlFromTxtFile(string txtFile)
{
// In a text file we are given the name, address and phone number of given person (each at a single line).
// Write a program, which creates new XML document, which contains these data in structured XML format.
using (var reader = new StreamReader(txtFile))
{
using (var writer = new XmlTextWriter(writeFileName, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.IndentChar = '\t';
writer.Indentation = 1;
writer.WriteStartDocument();
writer.WriteStartElement("data");
writer.WriteStartElement("person");
writer.WriteElementString("name", reader.ReadLine());
writer.WriteElementString("address", reader.ReadLine());
writer.WriteElementString("gsm", reader.ReadLine());
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
}
示例14: WriteAlbum
static void WriteAlbum(XmlTextWriter writer, string name, string artist)
{
writer.WriteStartElement("album");
writer.WriteElementString("name", name);
writer.WriteElementString("artist", artist);
writer.WriteEndElement();
}
示例15: save
public static bool save(SettingsModel settings)
{
bool result;
try
{
using (XmlTextWriter xmlTextWriter = new XmlTextWriter("settings.xml", Encoding.UTF8))
{
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.Indentation = 4;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("settings");
xmlTextWriter.WriteElementString("ip", settings.ip);
xmlTextWriter.WriteElementString("password", Cryptography.Encrypt(settings.password));
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
}
result = true;
}
catch (Exception exception)
{
LogManager.WriteToLog(exception.Message);
result = false;
}
return result;
}