本文整理汇总了C#中System.Xml.Linq.XElement.Save方法的典型用法代码示例。如果您正苦于以下问题:C# XElement.Save方法的具体用法?C# XElement.Save怎么用?C# XElement.Save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XElement
的用法示例。
在下文中一共展示了XElement.Save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Trans
private void Trans(XElement serializedExpression)
{
serializedExpression.Save("beforeTransit.xml");
Console.WriteLine("beforeTransit:");
Console.WriteLine(serializedExpression.ToString());
Transit(serializedExpression);
Console.WriteLine("afterTransit:");
Console.WriteLine(serializedExpression.ToString());
serializedExpression.Save("afterTransit.xml");
}
示例2: SaveXml
/// <summary>
/// 保存xml对象至文件
/// </summary>
/// <param name="xe">要保存的内容</param>
/// <param name="xmlName">保存的文件名(不含扩展名)</param>
/// <param name="isFullXmlName">True=程序内配置路径地址,只含文件名(不含扩展名)====False=文件绝对地址</param>
public void SaveXml(XElement xe, string xmlName, bool isFullXmlName)
{
if (isFullXmlName)
{
CreatNewDirTry();
xe.Save(GetXMLFileFullName(xmlName));
}
else
{
xe.Save(GetXMLFileFullName(xmlName));
}
}
示例3: Export
public static void Export(List<Dictionary<string, string>> data, string savepath, string format)
{
if (format == "JSON")
{
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
File.WriteAllText(savepath, json);
}
else if (format == "XML")
{
XElement root = new XElement("Items");
foreach (var item in data)
{
XElement itemEle = new XElement("Item");
itemEle.Add(item.Select(kv => new XElement(kv.Key, kv.Value)));
root.Add(itemEle);
}
root.Save(savepath);
}
else if (format == "CSV")
{
string res = "";
foreach (var item in data)
{
res += String.Join(", ",item.Select(kv => string.Format("\"{0}\"", kv.Value)))+"\n";
}
File.WriteAllText(savepath, res);
}
}
示例4: Main
static void Main()
{
DirectoryInfo directory = new DirectoryInfo(@"../../../../02.XMLProcessingIn.NET");
XElement dirXml = new XElement("directory");
dirXml.Add(TraverseDirectory(directory));
dirXml.Save("../../../dirsX.xml");
}
示例5: Generate
public static void Generate()
{
using (var db = new SalesContext())
{
var sales = new XElement("sales");
foreach (var supermarket in db.Supermarkets)
{
var sale = new XElement("sale");
sale.SetAttributeValue("vendor", supermarket.Name);
var records = db.Records
.Where(x => x.Supermarket.Id == supermarket.Id)
.GroupBy(x => x.Date)
.OrderBy(x => x.Key)
.Select(x => new { Date = x.Key, Sum = x.Sum(y => y.Quantity * y.UnitPrice) });
foreach (var record in records)
{
var summary = new XElement("summary");
summary.SetAttributeValue("date", record.Date.ToShortDateString());
summary.SetAttributeValue("total-sum", record.Sum.ToString("00"));
sale.Add(summary);
}
sales.Add(sale);
}
sales.Save(FilePath);
//Console.WriteLine(File.ReadAllText(FilePath));
}
}
示例6: Main
static void Main()
{
// Creating person.txt
string textFilePath = "../../person.txt";
StreamWriter writer = new StreamWriter(textFilePath);
using (writer)
{
writer.WriteLine("Иван Иванов");
writer.WriteLine("Перущица, ул.'Освобождение', №1");
writer.WriteLine("+359888123456");
}
StreamReader reader = new StreamReader(textFilePath);
using (reader)
{
var person = new
{
Name = reader.ReadLine(),
Address = reader.ReadLine(),
Phone = reader.ReadLine()
};
XElement personXElement = new XElement("Person",
new XElement("Name", person.Name),
new XElement("Address", person.Address),
new XElement("Phone", person.Phone));
personXElement.Save("../../person.xml");
}
}
示例7: CreateXmlFile
// creates folder, .xml file, and inserts data
public static bool CreateXmlFile(string root, XElement fileSystemTree)
{
string pathString = System.IO.Path.Combine(root, "XMLFolder");
System.IO.Directory.CreateDirectory(pathString);
string fileName = "XMLFile.xml";
// Using Combine to add the file name to the path.
pathString = System.IO.Path.Combine(pathString, fileName);
if (!System.IO.File.Exists(pathString))
{
using (System.IO.FileStream fs = System.IO.File.Create(pathString))
{
fileSystemTree.Save(fs);
return true;
}
}
else
{
return false;
}
}
示例8: WriteToStreamAsync
public override Task WriteToStreamAsync(Type type, object value,
Stream writeStream, System.Net.Http.HttpContent content,
System.Net.TransportContext transportContext)
{
return Task.Factory.StartNew(() =>
{
if (typeof(IEnumerable<IData>).IsAssignableFrom(type))
{
Type elementType = type.GetGenericArguments().First();
var resultXElement = new XElement("Data");
foreach (var element in (value as IEnumerable))
{
resultXElement.Add(GetXNode(elementType.Name, element).Root);
}
resultXElement.Save(writeStream);
return;
}
XDocument xNode = GetXNode(type.Name, value);
xNode.Save(writeStream);
});
}
示例9: backupXML
protected override void backupXML(string plikXml,string serwis,string typ)
{
if (numKolekcja.Count > 0)
{
var xmlFile = new XElement(typ.Replace(' ', '_'), new XAttribute("Data", DateTime.Now.ToString("dd-MM-yyyy")));
foreach (var elem in numKolekcja)
{
var x = new XElement("Row",
new XAttribute("Nazwa", elem.Nazwa),
new XAttribute("Symbol", elem.Symbol),
new XAttribute("Data", elem.Data),
new XAttribute("Kurs", elem.Kurs),
new XAttribute("MaxMin", elem.MaxMin),
new XAttribute("Zmiana", elem.Zmiana),
new XAttribute("ZmianaProc", elem.ZmianaProc),
new XAttribute("Otwarcie", elem.Otwarcie),
new XAttribute("Odniesienie", elem.Odniesienie),
new XAttribute("Wolumen", elem.Wolumen),
new XAttribute("Obrot", elem.Obrot),
new XAttribute("Transakcje", elem.Transakcje));
xmlFile.Add(x);
}
xmlFile.Save(plikXml);
Loger.dodajDoLogaInfo(serwis + typ + messages.xmlOutOK);
}
else
Loger.dodajDoLogaError(serwis + typ + messages.xmlOutFail);
}
示例10: ValidateSave
public void ValidateSave()
{
WorldEntity world = createTestWorld();
XElement xElement = WorldTransformer.Instance.ToXElement(world, TransformerSettings.WorldNamespace + "World");
xElement.Save("unittestsave.xml", SaveOptions.OmitDuplicateNamespaces);
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, "WorldSchema1.xsd");
XDocument xDocument = new XDocument(xElement);
xDocument.AddAnnotation(SaveOptions.OmitDuplicateNamespaces);
xDocument.Save("unittest2.xml", SaveOptions.OmitDuplicateNamespaces);
XElement x1 = new XElement(TransformerSettings.WorldNamespace + "Outer");
x1.Add(new XElement(TransformerSettings.WorldNamespace + "Inner"));
x1.Save("unittest3.xml", SaveOptions.OmitDuplicateNamespaces);
string val = "";
xDocument.Validate(schemaSet, (o, vea) => {
val += o.GetType().Name + "\n";
val += vea.Message + "\n";
}, true);
Assert.AreEqual("", val);
}
示例11: ExitSave
public void ExitSave()
{
XElement xRoot = new XElement("ChosenPath");
XAttribute xPath = new XAttribute("Path", filepath);
xRoot.Add(xPath);
xRoot.Save("Config.xml");
}
示例12: Main
static void Main()
{
//Write a program to traverse given directory and write to a XML file its contents together with all subdirectories and files.
//Use tags <file> and <dir> with attributes. Use XDocument, XElement and XAttribute.
XElement directoryXml =
new XElement("root-dir",
new XAttribute("path", "C:/Example"),
new XElement("dir",
new XAttribute("name", "docs"),
new XElement("file",
new XAttribute("name", "tutorial.pdf")),
new XElement("file",
new XAttribute("name", "TODO.txt")),
new XElement("file",
new XAttribute("name", "Presentation.pptx"))),
new XElement("dir",
new XAttribute("name", "photos"),
new XElement("file",
new XAttribute("name", "friends.jpg")),
new XElement("file",
new XAttribute("name", "the_cake.jpg"))
)
);
Console.WriteLine(directoryXml);
directoryXml.Save("../../../directoryXml.xml");
}
示例13: SetUserRoles
public static void SetUserRoles(params UserRole[] roles)
{
var xml = new XElement("Roles",
roles.Select(role => new XElement("Role", role)));
xml.Save(TestUserRolesFilePath);
}
示例14: ConvertXElementToString
private static string ConvertXElementToString(XElement xElement)
{
if (xElement == null)
{
throw new ArgumentNullException("xElement");
}
// Convert the xml to a string.
string xmlResult;
using (var memoryStream = new MemoryStream())
{
using (var writer = new StreamWriter(memoryStream, Encoding.UTF8))
{
xElement.Save(writer);
}
xmlResult = Encoding.UTF8.GetString(memoryStream.ToArray());
}
if (!string.IsNullOrWhiteSpace(xmlResult) &&
xmlResult.StartsWith(ByteOrderMarkUtf8))
{
xmlResult = xmlResult.Remove(0, ByteOrderMarkUtf8.Length);
}
return xmlResult;
}
示例15: Main
public static void Main()
{
XElement directories = new XElement("directories");
DirectoiesTravers("../../", directories);
Console.WriteLine("New file is created with name - Directoies.xml");
directories.Save("../../Directoies.xml");
}