【问题标题】:Creating xml document from a list<string> in c#从 C# 中的列表 <string> 创建 xml 文档
【发布时间】:2015-07-22 12:12:39
【问题描述】:

我有一个包含不同提取 xml 的字符串列表“fetchXmlList”。我想使用这个列表创建 xml 文档。

Xml 文档如下:

<mappings>
 <main_fetchxml>
    fetchXmlList[0]
 </main_fetchxml>
 <relatedqueries>
    fetchXmlList[1]
    fetchXmlList[2]
         .
         .
         .
 </relatedqueries>
</mappings>

对于相关查询 xml 节点,我将不得不添加 foreach 循环并从第二个列表项开始迭代每个列表项,直到列表末尾。

我开始编写以下代码行:

 XmlDocument fetchXmlDoc = new XmlDocument();
 XmlElement rootNode = fetchXmlDoc.CreateElement("main_fetchxml");
 fetchXmlDoc.AppendChild(rootNode);
 rootNode.AppendChild(fetchXmlList[0]);

但不允许将列表项附加到 XmlElement 对象。有没有其他办法?

任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 到目前为止你写了什么代码?
  • 可以编辑您的原始问题以包含代码示例吗? cmets中的代码很难阅读。
  • 使用XmlDocument吗?如果您可以使用 LINQ to XML,这将变得微不足道...尽管我会质疑您在这样的单个元素中使用多个值,在文本节点中使用换行符...
  • "但是不允许将列表项附加到 XmlElement 对象。" 你是什么意思?您是否收到错误消息?编译时间还是执行时间?请澄清。
  • 你需要一个根元素,不可能有两个xml根元素

标签: c# xml xmldocument


【解决方案1】:

你可以用这个:

public static void SerializeObject(this List<string> list, string fileName)
    {
        var serializer = new XmlSerializer(typeof(List<string>));
        using (var stream = File.OpenWrite(fileName))
        {
            serializer.Serialize(stream, list);
        }
    }

    public static void Deserialize(this List<string> list, string fileName)
    {
        var serializer = new XmlSerializer(typeof(List<string>));
        using (var stream = File.OpenRead(fileName))
        {
            var other = (List<string>)(serializer.Deserialize(stream));
            list.Clear();
            list.AddRange(other);
        }
    }

【讨论】:

    【解决方案2】:

    1) 你必须有一个根元素

    2) 试试这个代码

    public static string Fetch2Xml(string s)
    {      
        return HttpUtility.HtmlDecode(s);
    }
    
    private static void Main(string[] args)
    {
        String s = "&lt;fetch distinct=\"false\" no-lock=\"false\" mapping=\"logical\" /&gt;";
    
        String[] sa = new string[] { s, s, s, s, s, s, s };
    
        XmlDocument doc = new XmlDocument();
    
        XmlElement someRoot = doc.CreateElement("mappings");
    
        XmlElement ele = doc.CreateElement("main_fetchxml");
        ele.InnerXml = Fetch2Xml(sa[0]);
        someRoot.AppendChild(ele);
    
        XmlElement ele2 = doc.CreateElement("relatedqueries");
    
        for (int a = 1; a < sa.Length; ++a)
        {
            ele2.InnerXml += Fetch2Xml(sa[a]);
        }
    
        someRoot.AppendChild(ele2);
        doc.AppendChild(someRoot);
    
        doc.Save(@"c:\temp\bla.xml");
    }
    

    这也会导致:

    <mappings>
      <main_fetchxml>
        <fetch distinct="false" no-lock="false" mapping="logical" />
      </main_fetchxml>
      <relatedqueries>
        <fetch distinct="false" no-lock="false" mapping="logical" />
        <fetch distinct="false" no-lock="false" mapping="logical" />
        <fetch distinct="false" no-lock="false" mapping="logical" />
        <fetch distinct="false" no-lock="false" mapping="logical" />
        <fetch distinct="false" no-lock="false" mapping="logical" />
        <fetch distinct="false" no-lock="false" mapping="logical" />
      </relatedqueries>
    </mappings>
    

    【讨论】:

    • 你可以使用字符串生成器^^
    • 由于 fetchXmlList 包含 fetchxmls,以上代码将无法正确添加 xml 标签。例如:<fetch distinct="false" no-lock="false" mapping="logical"><此外,所有列表项都应正确格式化并形成格式化的 xml 文档。
    • 请在您之前的答案中将 InnerText 更改为 InnerXml。这对我有用。谢谢。
    • 你需要 fetch2xml 否则你会得到: <fetch distinct="false" no-lock="false" mapping="logical" /><fetch distinct="false" no-lock="false" mapping="logical" />< fetch distinct="false" no-lock="false" mapping="logical" /><fetch distinct="false" no-lock="false" mapping="logical" /><fetch distinct=" false" no-lock="false" mapping="logical" /><fetch distinct="false" no-lock="false" mapping="logical" />
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    相关资源
    最近更新 更多