【问题标题】:I am using LINQ to connect to a database and i get a list of properties and I want to convert it to an XML File我正在使用 LINQ 连接到数据库,我得到一个属性列表,我想将其转换为 XML 文件
【发布时间】:2013-06-07 20:13:15
【问题描述】:

以下是我使用 linq 登录数据库的内容,然后我使用 C# 表达式收集我想要的数据。我想做的下一件事是将这些数据转换为 XML 任何想法?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Data;

namespace VcacManagementTools.BuildProfiles
{
    public static class BuildProfileTools
    {
        public static ICollection<string> GetExistingBuildProfileNames(string repositoryHostname,
                                                                       string repositoryUsername,
                                                                       string repositoryPassword)
        {

            var url = string.Format("https://{0}/repository/data/ManagementModelEntiti.svc", repositoryHostname);

            var managementModelClient = new DynamicOps.ManagementModel.ManagementModelEntities(new Uri(url))
                {
                    Credentials = new NetworkCredential(repositoryUsername, repositoryPassword)
                };



            return managementModelClient
                .GlobalProfiles
                .Select(gp => gp.ProfileName)
                .ToList();

我收到的输出是一个值列表

【问题讨论】:

  • 如果你熟悉 LINQ,何不试试 LINQ to XML,那里有很多很棒的工具,语法与其他 LINQ 功能相似。
  • 您是否在使用您在代码中声明的 DataTable?
  • @YuriyGalanter 抱歉,这是一个错误
  • @Evanlewis 抱歉,我是 C# 新手,对 LINQ 不太熟悉,但您知道有教程的网站吗?
  • 这里有很好的教程,您的需要请参阅第 2 节(或问题 9)。 LINQ to XML Tutorial

标签: c# xml linq


【解决方案1】:

如果我理解你的话,你想获取数据(列表包含数据库中的数据)并将其放入 XML 文件中。我使用变量来显示每个数据的放置位置。

如果你有一个 XML:

try
{
    doc = XDocument.Load(spath, LoadOptions.SetBaseUri);
    foreach(String propertyData in dataList)
    {
        XElement root = new XElement(ElementName);
        root.Add(new XElement("property1", propertyData));
        doc.Element(MainElement).Add(root);
    }
    doc.Save(spath);
}
catch (Exception)
{

}

【讨论】:

    猜你喜欢
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    相关资源
    最近更新 更多