gpt4 book ai didi

xml - 使用AutoMapper将特定的XML元素从字符串分配给ViewModel

转载 作者:行者123 更新时间:2023-12-03 17:04:25 31 4
gpt4 key购买 nike

我有一个数据库表,该表在字符串类型的字段中存储XML数据块。我想将XML的某些元素提取到自定义ViewModel中。

在此的示例ErrorTableModel.ErrorXML保存示例XML字符串。我正在尝试从该字符串中获取“ message”元素,并将其映射到ErrorViewModel.message中。

使用AutoMapper是否可以完成?

样本字符串。此数据存储在ErrorTableModel的ErrorXML属性中:

<error
application="TestApp"
type="System.DivideByZeroException"
message="Something wicked this way comes."
</error>


数据库模型:

public class ErrorTableModel
{
public int ErrorId { get; set; }
public string ErrorXml { get; set; }
}


自定义ViewModel:

public class ErrorViewModel
{
public int id { get; set; }
public string application { get; set; }
public string type { get; set; }
public string message { get; set; }
}


更新:
我创建了一个新的帮助器类,旨在帮助将XML分开。

protected override T ResolveCore(XElement source)
{
if (source == null || string.IsNullOrEmpty(source.Value))
{
return default(T);
}

return (T)Convert.ChangeType(source.Value, typeof(T));
}


当我从ErrorViewModel执行映射时,我试图引用该方法。

    Mapper.CreateMap<XElement, ErrorViewModel>()
.ForMember(
dest => dest.ErrorXml,
options => options.ResolveUsing<XElementResolver<string>>()
.FromMember(source => source.Element("error")
.Descendants("message").Single()));


不幸的是,这行不通...但是我想我已经接近了。

更新2:

为了澄清起见,我希望得到的数据看起来像这样:

  ErrorViewModel.application = "TestApp"
ErrorViewModel.type = "System.DivideByZeroException"
ErrorViewModel.message = "Something wicked this way comes."

最佳答案

像这样:

public class ErrorConverter1 : ITypeConverter<ErrorTableModel, ErrorViewModel>
{
public ErrorViewModel Convert(ResolutionContext context)
{
var dbModel = context.SourceValue as ErrorTableModel;

if (dbModel == null)
return null;

var xDocument = XDocument.Parse(dbModel.ErrorXml);

var errorElement = xDocument.Descendants(XName.Get("error")).Single();

return new ErrorViewModel()
{
id = dbModel.ErrorId,
application = errorElement.Attribute(XName.Get("application")).Value,
type = errorElement.Attribute(XName.Get("type")).Value,
message = errorElement.Attribute(XName.Get("message")).Value
};
}
}


或通过XML反序列化。为此,您需要将 ErrorViewModel类更改为:

[XmlRoot("error")]
public class ErrorViewModel
{
public int id { get; set; }

[XmlAttribute("application")]
public string application { get; set; }

[XmlAttribute("type")]
public string type { get; set; }

[XmlAttribute("message")]
public string message { get; set; }
}

public class ErrorConverter2 : ITypeConverter<ErrorTableModel, ErrorViewModel>
{
public ErrorViewModel Convert(ResolutionContext context)
{
var dbModel = context.SourceValue as ErrorTableModel;

if (dbModel == null)
return null;

var serializer = new XmlSerializer(typeof(ErrorViewModel));

var result = (ErrorViewModel) serializer.Deserialize(new StringReader(dbModel.ErrorXml));

result.id = dbModel.ErrorId;

return result;
}
}


用法是这样的:

public class MappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<ErrorTableModel, ErrorViewModel>()
.ConvertUsing<ErrorConverter1>()//or use the ErrorConverter2
;
}
}

class Program
{
static void Main(string[] args)
{
Mapper.Initialize(x => x.AddProfile<MappingProfile>());

var dbModel = new ErrorTableModel()
{
ErrorId = 1,
ErrorXml =
"<error application=\"TestApp\" type=\"System.DivideByZeroException\" message=\"Something wicked this way comes.\"></error>"
};

var viewModel = Mapper.Map<ErrorTableModel, ErrorViewModel>(dbModel);

Console.WriteLine(viewModel.id);
Console.WriteLine(viewModel.application);
Console.WriteLine(viewModel.type);
Console.WriteLine(viewModel.message);

}
}


根据需要添加保护条款。

关于xml - 使用AutoMapper将特定的XML元素从字符串分配给ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19594156/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com