XDocument 元素 - 删除标签
XDocument Elements - Remove tags
我目前正在将有效 XML 文件中的值导入到 SQL 数据库中。以下是 xml.
的片段
<Suppliers>
<SupplierDetails>
<Name>Some Name</Name>
<Logo>my-logo.jpg</Logo>
<ID>4070</ID>
<DateCertified>14/10/2016 00:00:00</DateCertified>
<Description>Some description</Description>
<Brands>Brand1</Brands>
<Brands>Brand2</Brands>
<Brands>Brand3</Brands>
<Brands>Brand4</Brands>
<SubBrands>SubBrand1</SubBrands>
<SubBrands>SubBrand2</SubBrands>
<SubBrands>SubBrand3</SubBrands>
<SubBrands>HVAC</SubBrands>
</SupplierDetails>
</Suppliers>
我遇到的问题是品牌和子品牌列表。所有其他值(如名称、描述等)都可以,并且返回时没有它们的元素 "tags"。但是,品牌/子品牌看起来像这样。
<Brands>Brand1</Brands>
我只需要 "Brand1" 位。
这就是我绑定 ViewModel 的方式
var model =
from xml in xmlDoc.Descendants("SupplierDetails")
select new SupplierViewModel
{
Name = xml.Element("Name").ToString(),
Logo = xml.Element("Logo").ToString(),
Supplier_Id = (int)xml.Element("ID"),
DateCertified = xml.Element("DateCertified").ToString(),
Description = xml.Element("Description").ToString(),
Brands = (from b in xml.Elements("Brands") select b.ToString()).ToList(),
SubBrands = (from sb in xml.Elements("SubBrands") select sb.ToString()).ToList()
};
还有我的 ViewModel
public class SupplierViewModel
{
public int Id { get; set; }
// XML Elements
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("LOGO")]
public string Logo { get; set; }
[XmlElement("ID")]
public int Supplier_Id { get; set; }
[XmlElement("DateCertified")]
public string DateCertified { get; set; }
[XmlElement("Description")]
public string Description { get; set; }
[XmlElement("Brands")]
public List<string> Brands { get; set; }
[XmlElement("SubBrands")]
public List<string> SubBrands { get; set; }
}
我知道我可以使用一些正则表达式来删除标签,但是有没有其他方法可以即时执行此操作...?
提前致谢。 :)
应该足以阅读 .Value
而不是使用 .ToString()
string xmlString = "<Suppliers><SupplierDetails><Name>Some Name</Name><Brands>Brand1</Brands><Brands>Brand2</Brands></SupplierDetails></Suppliers>";
var xmlDoc = XDocument.Parse(xmlString);
var model =
from xml in xmlDoc.Descendants("SupplierDetails")
select new SupplierViewModel {
Name = xml.Element("Name").ToString(),
Brands = (from b in xml.Elements("Brands") select b.Value).ToList(),
};
var YourModel = model.ToList()[0];
我目前正在将有效 XML 文件中的值导入到 SQL 数据库中。以下是 xml.
的片段<Suppliers>
<SupplierDetails>
<Name>Some Name</Name>
<Logo>my-logo.jpg</Logo>
<ID>4070</ID>
<DateCertified>14/10/2016 00:00:00</DateCertified>
<Description>Some description</Description>
<Brands>Brand1</Brands>
<Brands>Brand2</Brands>
<Brands>Brand3</Brands>
<Brands>Brand4</Brands>
<SubBrands>SubBrand1</SubBrands>
<SubBrands>SubBrand2</SubBrands>
<SubBrands>SubBrand3</SubBrands>
<SubBrands>HVAC</SubBrands>
</SupplierDetails>
</Suppliers>
我遇到的问题是品牌和子品牌列表。所有其他值(如名称、描述等)都可以,并且返回时没有它们的元素 "tags"。但是,品牌/子品牌看起来像这样。
<Brands>Brand1</Brands>
我只需要 "Brand1" 位。
这就是我绑定 ViewModel 的方式
var model =
from xml in xmlDoc.Descendants("SupplierDetails")
select new SupplierViewModel
{
Name = xml.Element("Name").ToString(),
Logo = xml.Element("Logo").ToString(),
Supplier_Id = (int)xml.Element("ID"),
DateCertified = xml.Element("DateCertified").ToString(),
Description = xml.Element("Description").ToString(),
Brands = (from b in xml.Elements("Brands") select b.ToString()).ToList(),
SubBrands = (from sb in xml.Elements("SubBrands") select sb.ToString()).ToList()
};
还有我的 ViewModel
public class SupplierViewModel
{
public int Id { get; set; }
// XML Elements
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("LOGO")]
public string Logo { get; set; }
[XmlElement("ID")]
public int Supplier_Id { get; set; }
[XmlElement("DateCertified")]
public string DateCertified { get; set; }
[XmlElement("Description")]
public string Description { get; set; }
[XmlElement("Brands")]
public List<string> Brands { get; set; }
[XmlElement("SubBrands")]
public List<string> SubBrands { get; set; }
}
我知道我可以使用一些正则表达式来删除标签,但是有没有其他方法可以即时执行此操作...?
提前致谢。 :)
应该足以阅读 .Value
而不是使用 .ToString()
string xmlString = "<Suppliers><SupplierDetails><Name>Some Name</Name><Brands>Brand1</Brands><Brands>Brand2</Brands></SupplierDetails></Suppliers>";
var xmlDoc = XDocument.Parse(xmlString);
var model =
from xml in xmlDoc.Descendants("SupplierDetails")
select new SupplierViewModel {
Name = xml.Element("Name").ToString(),
Brands = (from b in xml.Elements("Brands") select b.Value).ToList(),
};
var YourModel = model.ToList()[0];