Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将XML文档解析为对象模型?_C#_Xml - Fatal编程技术网

C# 如何将XML文档解析为对象模型?

C# 如何将XML文档解析为对象模型?,c#,xml,C#,Xml,我需要将xml文档解析为我创建的对象模型,但我不知道如何做到这一点,我认为这是因为我对xml结构缺乏理解 我试着从文档中获取所有元素,并根据它们的属性(我认为它们被调用)从每个元素创建单独的对象 这是我的C#代码: 下面是XML数据: <?xml version="1.0" encoding="UTF-8"?> <manifest version="1.0.0" totalbytes="6131797"> <source uri="codeapi.io/Game

我需要将xml文档解析为我创建的对象模型,但我不知道如何做到这一点,我认为这是因为我对xml结构缺乏理解

我试着从文档中获取所有元素,并根据它们的属性(我认为它们被调用)从每个元素创建单独的对象

这是我的C#代码:

下面是XML数据:

<?xml version="1.0" encoding="UTF-8"?>
<manifest version="1.0.0" totalbytes="6131797">
  <source uri="codeapi.io/Game/patches/">
    <file resource="FooGame.sln" size="1125" checksum="530B9F1C2412A6D74EF017919074FD8966E5280D" />
    <file resource=".vs\FooGame\v16\.suo" size="69120" checksum="438976A3681FDD503DB4FBFCBB5D420E9E8838DD" />
  </source>
</manifest>

我花了很多时间在一个通过XML模式解析的类似应用程序上,我发现最简单的方法是将XML文档转换为XmlNodeList。从这里,您可以使用SelectNodes和SelectSingleNodes在其中导航。看看这个:,但基本上您要做的是创建一个xpath字符串,用于选择所需的节点。这里有一些关于这方面的文档:

就像我们有JSON,我们有XML。可能还有很多其他网站会这样做

粘贴您的XML,它将生成以下内容:

    [XmlRoot(ElementName="file")]
    public class File {
        [XmlAttribute(AttributeName="resource")]
        public string Resource { get; set; }
        [XmlAttribute(AttributeName="size")]
        public string Size { get; set; }
        [XmlAttribute(AttributeName="checksum")]
        public string Checksum { get; set; }
    }

    [XmlRoot(ElementName="source")]
    public class Source {
        [XmlElement(ElementName="file")]
        public List<File> File { get; set; }
        [XmlAttribute(AttributeName="uri")]
        public string Uri { get; set; }
    }

    [XmlRoot(ElementName="manifest")]
    public class Manifest {
        [XmlElement(ElementName="source")]
        public Source Source { get; set; }
        [XmlAttribute(AttributeName="version")]
        public string Version { get; set; }
        [XmlAttribute(AttributeName="totalbytes")]
        public string Totalbytes { get; set; }
    }
一旦您将数据反序列化为某些内容,剩下的就容易多了。您可以使用自动生成的模型,也可以将其映射到自己的模型

使用LINQ

c#

void Main()
{
字符串文件名=@“e:\Temp\GamePatches.xml”;
XDocument manifest=XDocument.Load(文件名);
字符串版本=manifest.Root.Attribute(“version”).Value;
列表模型=新列表();
foreach(manifest.substands(“文件”)中的元素e)
{
添加(新的manifestModel(){Version=Version
,Resource=(字符串)e.Attribute(“Resource”).Value
,Size=(字符串)e.Attribute(“Size”).Value
,校验和=(字符串)e.Attribute(“校验和”).Value}
);
}
}
//在此处定义其他方法和类
公共类模型
{
公共字符串版本{get;set;}
公共字符串资源{get;set;}
公共字符串大小{get;set;}
公共字符串校验和{get;set;}
}

如果要获得版本号,可能需要重复的版本号吗?您好,请查看以下内容:。这应该有助于你了解这一点。Linq可能是你最好的选择
    [XmlRoot(ElementName="file")]
    public class File {
        [XmlAttribute(AttributeName="resource")]
        public string Resource { get; set; }
        [XmlAttribute(AttributeName="size")]
        public string Size { get; set; }
        [XmlAttribute(AttributeName="checksum")]
        public string Checksum { get; set; }
    }

    [XmlRoot(ElementName="source")]
    public class Source {
        [XmlElement(ElementName="file")]
        public List<File> File { get; set; }
        [XmlAttribute(AttributeName="uri")]
        public string Uri { get; set; }
    }

    [XmlRoot(ElementName="manifest")]
    public class Manifest {
        [XmlElement(ElementName="source")]
        public Source Source { get; set; }
        [XmlAttribute(AttributeName="version")]
        public string Version { get; set; }
        [XmlAttribute(AttributeName="totalbytes")]
        public string Totalbytes { get; set; }
    }
var serializer = new XmlSerializer(typeof(Manifest), new XmlRootAttribute("manifest"));
using (var stream = System.IO.File.OpenRead("test.xml"))
{
    var deserialized = (Manifest)serializer.Deserialize(stream);
}
void Main()
{
    string fileName = @"e:\Temp\GamePatches.xml";

    XDocument manifest = XDocument.Load(fileName);
    string version = manifest.Root.Attribute("version").Value;

    List<ManifestModel> manifestModel = new List<ManifestModel>();

    foreach (XElement e in manifest.Descendants("file"))
    {
        manifestModel.Add(new ManifestModel() { Version = version
                , Resource = (string)e.Attribute("resource").Value 
                , Size = (string)e.Attribute("size").Value 
                , Checksum = (string)e.Attribute("checksum").Value }
                );
    }
}

// Define other methods and classes here
public class ManifestModel
{
    public string Version { get; set; }
    public string Resource { get; set; }
    public string Size { get; set; }
    public string Checksum { get; set; }
}