Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# 如何为此创建序列化类?_C#_.net_Xml_Xml Serialization - Fatal编程技术网

C# 如何为此创建序列化类?

C# 如何为此创建序列化类?,c#,.net,xml,xml-serialization,C#,.net,Xml,Xml Serialization,我有一些类似的东西(很抱歉这些坏名字) 在根行上引发异常,表示此内容有错误。所以我不知道它是不是对schemaLocation很生气,因为我现在正在使用本地主机还是什么 错误 下面是一个基于您提供的信息的简单示例。基本上,您需要为另一个包含字符串的元素创建一个单独的类 您可以使用来精确控制类解析Xml元素的方式,这基本上将类属性映射到Xml文件中的元素/属性。例如,由于您提供的示例Xml中的文档元素是root,因此我明确定义MyClass有一个名为root的文档元素来匹配您的Xml。默认情况下,

我有一些类似的东西(很抱歉这些坏名字)

在根行上引发异常,表示此内容有错误。所以我不知道它是不是对schemaLocation很生气,因为我现在正在使用本地主机还是什么

错误


下面是一个基于您提供的信息的简单示例。基本上,您需要为
另一个包含
字符串的元素
创建一个单独的类

您可以使用来精确控制类解析Xml元素的方式,这基本上将类属性映射到Xml文件中的元素/属性。例如,由于您提供的示例Xml中的文档元素是
root
,因此我明确定义
MyClass
有一个名为
root
的文档元素来匹配您的Xml。默认情况下,序列化程序将查找名为
MyClass
的元素,如果省略它,反序列化方法将抛出

这将有助于您继续:

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("root")]
public class MyClass
{
    public MyClass()
    {

    }

    public string product { get; set; }

    [XmlElement("SomeHighLevelElement")]
    public List<SomeHighLevelElement> ListWrapper { get; set; }

}

public class SomeHighLevelElement
{
    public AnotherElment anotherElment { get; set; }
}

public class AnotherElment
{
    public string lowestElement { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用System.Xml;
使用System.Xml.Serialization;
[XmlRoot(“根”)]
公共类MyClass
{
公共MyClass()
{
}
公共字符串乘积{get;set;}
[XmlElement(“SomeHighLevelElement”)]
公共列表ListWrapper{get;set;}
}
公共类高级元素
{
公共anothelment anothelment{get;set;}
}
公营安老院
{
公共字符串下限元素{get;set;}
}
以及基于您提供的Xml的示例测试方法:

using System.Xml;
using System.Xml.Serialization;
using System.IO;
.
.
.
public void Test()
{
    string xml = @"<root>
                  <product>product name</product>
                  <SomeHighLevelElement>
                    <anotherElment>
                      <lowestElement>foo</lowestElement>
                    </anotherElment>
                  </SomeHighLevelElement>
                  <SomeHighLevelElement>
                    <anotherElment>
                      <lowestElement>bar</lowestElement>
                    </anotherElment>
                  </SomeHighLevelElement>
                  <SomeHighLevelElement>
                    <anotherElment>
                      <lowestElement>baz</lowestElement>
                    </anotherElment>
                  </SomeHighLevelElement>
                </root>";
    MyClass c = Deserialize<MyClass>(xml);
}

public T Deserialize<T>(string xml)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    return (T)serializer.Deserialize(new StringReader(xml));
}
使用System.Xml;
使用System.Xml.Serialization;
使用System.IO;
.
.
.
公开无效测试()
{
字符串xml=@“
产品名称
福
酒吧
巴兹
";
MyClass c=反序列化(xml);
}
公共T反序列化(字符串xml)
{
XmlSerializer serializer=新的XmlSerializer(typeof(T));
反序列化(新的StringReader(xml));
}

Ah我的当前版本很接近。但是,现在我的xml文档中出现了一个错误。我将在我的原始帖子中发布我所拥有的。还有一件事我如何使它有效。例如,我从导入的xml文档中删除了我的产品标签。我原以为它会抛出一个例外,但它继续它的快乐方式。如果它丢失了,我希望它抛出一个异常。我该怎么做。发布实际的XML,而不仅仅是你认为可能相关的摘录。好的,我用我认为会崩溃的行更新了最初的XML,因为这是它崩溃的行。它只是说错误XML2,2。如果这真的是您的Xml,那么它的格式是错误的;
xsi:schemaLocation
缺少一个结束引号(它还有一个空格,通常不应该在那里)。该引号只是一个复制粘贴错误,但我更正了多余的空格。我仍然认为这是一个路径错误。就像我通过验证器运行xml一样,唯一出现的是文档
Schema.xsd
无法加载!那么,我现在是否要将位置更改为本地主机路径呢?在visual Studio中,它会得出这样的结论:“文档中从该位置引用的架构包含错误。”但它不会告诉我它有什么问题,当我使用一些在线工具验证我的架构时,它会说它是有效的。
xmlns="http://www.domain.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.Domain.com Schema.xsd
System.InvalidOperationException was caught
  Message="There is an error in XML document (2, 2)."
  Source="System.Xml"
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("root")]
public class MyClass
{
    public MyClass()
    {

    }

    public string product { get; set; }

    [XmlElement("SomeHighLevelElement")]
    public List<SomeHighLevelElement> ListWrapper { get; set; }

}

public class SomeHighLevelElement
{
    public AnotherElment anotherElment { get; set; }
}

public class AnotherElment
{
    public string lowestElement { get; set; }
}
using System.Xml;
using System.Xml.Serialization;
using System.IO;
.
.
.
public void Test()
{
    string xml = @"<root>
                  <product>product name</product>
                  <SomeHighLevelElement>
                    <anotherElment>
                      <lowestElement>foo</lowestElement>
                    </anotherElment>
                  </SomeHighLevelElement>
                  <SomeHighLevelElement>
                    <anotherElment>
                      <lowestElement>bar</lowestElement>
                    </anotherElment>
                  </SomeHighLevelElement>
                  <SomeHighLevelElement>
                    <anotherElment>
                      <lowestElement>baz</lowestElement>
                    </anotherElment>
                  </SomeHighLevelElement>
                </root>";
    MyClass c = Deserialize<MyClass>(xml);
}

public T Deserialize<T>(string xml)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    return (T)serializer.Deserialize(new StringReader(xml));
}