XML解析 - 解析2个相同的命名XMLArrays

时间:2015-06-25 06:28:23

标签: c# xml xml-parsing

我实际上已经搜索了但是没有找到适合我的要求的解决方案。我要做的是将.csproj文件的一些信息解析为c#对象。

我使用此功能来执行此操作。只要我将文件表示为c#对象来进行转换,该方法就会使posibilbity解析任何xmlFile。

public static Object  DeserializeXml(string xmlPath)
    {    
        if (File.Exists(xmlPath))
        {
            object returnObject;
            XmlSerializer serializer = new XmlSerializer(typeof (ProjectConfiguration));

            using (FileStream fs = new FileStream(xmlPath, FileMode.Open))
            {
                XmlReader reader = XmlReader.Create(fs);
                returnObject = serializer.Deserialize(reader);
                fs.Close();
            }
            return returnObject;
        }
        return null;
    }

这是问题所在。 csproj文件显示类似的XmlArrays。

<ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
</ItemGroup>  
<ItemGroup>
   <Compile Include="Class1.cs" />
   <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>enter code here

有没有想过如何在c#类文件中注释? 这会引发错误......

    [XmlArray("ItemGroup")]
    [XmlArrayItem("Compile")]
    [XmlAttribute("Include")]
    public List<string> ItemGroupCompileIncludeSurrogate
    {
        get   {...}

        set   {...}
    }
    [XmlArray("ItemGroup")]
    [XmlArrayItem("Reference ")]
    [XmlAttribute("Include")]
    public List<string> ItemGroupCompileIncludeSurrogate
    {
        get   {...}

        set   {...}
    }

谢谢你们所有人的预测! ; - )

[编辑] 使用第一个想法后 - &gt;谢谢:-)看起来像这样

<ItemGroup>
   <Reference Include="System" />
   <Reference Include="System.Core" />
   <Compile Include="Class1.cs" />
   <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

Greez Iki

2 个答案:

答案 0 :(得分:1)

尝试:

public class Root
{
    [XmlElement("ItemGroup")]
    public List<ItemGroup> ItemGroups { get; set; }
}

public class ItemGroup
{
    [XmlElement("Reference")]
    public List<ReferenceOrCompile> References { get; set; }

    [XmlElement("Compile")]
    public List<ReferenceOrCompile> Compiles { get; set; }
}

public class ReferenceOrCompile
{
    [XmlAttribute("Include")]
    public string Include { get; set; }
}

如果您想将对象转换为xml,请使用:

var item1 = new ReferenceOrCompile() { Include = "System" };
var item2 = new ReferenceOrCompile() { Include = "System.Core" };
var item3 = new ReferenceOrCompile() { Include = "Class1.cs" };
var item4 = new ReferenceOrCompile() { Include = "Properties\\AssemblyInfo.cs" };

var group1 = new ItemGroup()
{
    References = new List<ReferenceOrCompile>()
    {
        item1, item2
    }
};
var group2 = new ItemGroup()
{
    Compiles = new List<ReferenceOrCompile>()
    {
        item3, item4
    }
};

var root = new Root()
{
    ItemGroups = new List<ItemGroup>()
    {
        group1, group2
    }
}

然后序列化(根)

答案 1 :(得分:1)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml"; 
        static void Main(string[] args)
        {

            Root root = new Root()
            {
                itemGroup = new List<ItemGroup>() {
                     new ItemGroup(){
                         reference = new List<Reference>() {
                             new Reference(){ include = "System"},
                             new Reference(){ include= "System.Core"}
                         }
                     },
                     new ItemGroup(){
                         compile = new List<Compile>(){
                             new Compile() { include = "Class1.cs"},
                             new Compile() { include = "Properties\\AssemblyInfo.cs"}
                         }
                     }
                }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(Root));

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, root);
            writer.Flush();
            writer.Close();
            writer.Dispose();

            XmlSerializer xs = new XmlSerializer(typeof(Root));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            Root newRoot = (Root)xs.Deserialize(reader);



        }
    }
    [XmlRoot("Root")]
    public class Root
    {
        [XmlElement("ItemGroup")]
        public List<ItemGroup> itemGroup { get; set; }
    }
    [XmlRoot("ItemGroup")]
    public class ItemGroup
    {
        [XmlElement("Compile")]
        public List<Compile> compile { get; set; }
        [XmlElement("Reference")]
        public List<Reference> reference { get; set; }
    }
    [XmlRoot("Compile")]
    public class Compile
    {
        [XmlAttribute("Include")]
        public string include { get; set; }
    }
    [XmlRoot("Reference")]
    public class Reference
    {
        [XmlAttribute("Include")]
        public string include { get; set; }
    }

}
​