在我的代码中,我已将SVG(XML)文件转换为C#对象,我想将XML数据插入数据库中但无法插入
下面是代码
try
{
string uri = @"C:\Users\iqra\Desktop\SVGXMLToJsonApp\SVGXMLToJsonApp\File\3rect.svg";
XmlSerializer serializer = new XmlSerializer(typeof(Svg));
using (TextReader reader = new StreamReader(uri))
{
Svg result = (Svg)serializer.Deserialize(reader);
//Console.WriteLine(result);
List<Rect> List2 = new List<Rect>();
string con = "Data Source=.;Initial Catalog=FloorPlan;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(con))
{
string query = "select * from Stand_Details";
SqlCommand cmd1 = new SqlCommand(query, conn);
SqlDataReader dr;
conn.Open();
dr = cmd1.ExecuteReader();
while (dr.Read())
{
Rect newItem = new Rect();
newItem.Id = dr["Id"] == DBNull.Value ? null : dr["Id"].ToString();
newItem.X = dr["x"] == DBNull.Value ? null : dr["x"].ToString();
newItem.Y = dr["y"] == DBNull.Value ? null : dr["y"].ToString();
newItem.Class = dr["Class"] == DBNull.Value ? null : dr["Class"].ToString();
newItem.Height = dr["Height"] == DBNull.Value ? null : dr["Height"].ToString();
newItem.Width = dr["Width"] == DBNull.Value ? null : dr["Width"].ToString();
List2.Add(newItem);
conn.Close();
}
}
List<Rect> thirdlist = new List<Rect>();
foreach (var item1 in result)
{
bool isMatch = false;
foreach (var item2 in List2)
{
//if (List1.SequenceEqual(List2))
if (item1.Id == item2.Id && item1.X == item2.X && item1.Y == item2.Y && item1.Class == item2.Class && item1.Height == item2.Height && item1.Width == item2.Width)
{
isMatch = true;
Console.WriteLine("Record Exist");
}
else
{
try
{
SqlConnection cnn = new SqlConnection(con);
cnn.Open();
string update = "SET ANSI_WARNINGS OFF;Update Stand_Details set Id=@Id,x=@x,y=@y,Class=@Class,Height=@Height,Width=@Width where Id=@Id";
using (SqlCommand cmd = new SqlCommand(update, cnn))
{
//Loop through the and get of parameter values
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", item1.Id ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@x", item1.X ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@y", item1.Y ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@Class", item1.Class ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@Height", item1.Height ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@Width", item1.Width ?? (object)DBNull.Value);
cmd.ExecuteNonQuery();
isMatch = true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
break;
}
}
if (!isMatch)
{
thirdlist.Add(item1);
}
}
foreach (Rect p in thirdlist)
{
try
{
//Create SQL conection to your database here
using (SqlConnection cnn = new SqlConnection(con))
{
// Open your connection
cnn.Open();
//Change the table name here
string sql = "SET ANSI_WARNINGS OFF;INSERT INTO Stand_Details(Id,Class, Width, Height,x,y) VALUES (@Id,@Class, @Width, @Height,@x,@y)";
// Create the Command and Parameter objects.
using (SqlCommand cmd = new SqlCommand(sql, cnn))
{
//Loop through the and get of parameter values
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", p.Id ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@Class", p.Class ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@Width", p.Width ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@Height", p.Height ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@x", p.X ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@y", p.Y ?? (object)DBNull.Value);
//Execute the query
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
string errorMsg = ex.Message.ToString();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
下面是我的课程
[XmlRoot(ElementName = "style", Namespace = "http://www.w3.org/2000/svg")]
public class Style
{
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "rect", Namespace = "http://www.w3.org/2000/svg")]
public class Rect
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "x")]
public string X { get; set; }
[XmlAttribute(AttributeName = "y")]
public string Y { get; set; }
[XmlAttribute(AttributeName = "class")]
public string Class { get; set; }
[XmlAttribute(AttributeName = "width")]
public string Width { get; set; }
[XmlAttribute(AttributeName = "height")]
public string Height { get; set; }
}
[XmlRoot(ElementName = "g", Namespace = "http://www.w3.org/2000/svg")]
public class G
{
[XmlElement(ElementName = "rect", Namespace = "http://www.w3.org/2000/svg")]
public Rect Rect { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlElement(ElementName = "text", Namespace = "http://www.w3.org/2000/svg")]
public Text Text { get; set; }
}
[XmlRoot(ElementName = "text", Namespace = "http://www.w3.org/2000/svg")]
public class Text
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "transform")]
public string Transform { get; set; }
[XmlAttribute(AttributeName = "class")]
public string Class { get; set; }
[XmlText]
public string Text1 { get; set; }
}
[XmlRoot(ElementName = "svg", Namespace = "http://www.w3.org/2000/svg")]
public class Svg
{
[XmlElement(ElementName = "style", Namespace = "http://www.w3.org/2000/svg")]
public Style Style { get; set; }
[XmlAttribute(AttributeName = "style")]
public string _Style { get; set; }
[XmlElement(ElementName = "g", Namespace = "http://www.w3.org/2000/svg")]
public List<G> G { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName = "xlink", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xlink { get; set; }
[XmlAttribute(AttributeName = "x")]
public string X { get; set; }
[XmlAttribute(AttributeName = "y")]
public string Y { get; set; }
[XmlAttribute(AttributeName = "viewBox")]
public string ViewBox { get; set; }
[XmlAttribute(AttributeName = "space", Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Space { get; set; }
}
以下是我要插入数据库中的XML数据
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 22.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="_x30_" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1366 768" style="enable-background:new 0 0 1366 768;" xml:space="preserve">
<style type="text/css">
.st0{fill:none;stroke:#000000;stroke-width:0.7087;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}
.st1{fill:#105689;}
.st2{font-family:'ArialMT';}
.st3{font-size:56.5499px;}
.st4{fill:#4554A5;}
.st5{font-size:56.5574px;}
.st6{fill:#2776BB;}
</style>
<g id="LWPOLYLINE">
<rect id="idrect3" x="251.4" y="-0.3" class="st0" width="866" height="300.2"/>
</g>
<g id="LWPOLYLINE_1_">
<rect id="idrect2" x="248.6" y="366.5" class="st0" width="500.3" height="400.2"/>
</g>
<g id="LWPOLYLINE_2_">
<rect id="idrect1" x="811.4" y="364.2" class="st0" width="300.2" height="404.1"/>
</g>
<g id="TEXT">
<text id="idnano" transform="matrix(1 0 0 1 515.7997 166.1773)" class="st1 st2 st3">Nano Tech</text>
</g>
<g id="TEXT_1_">
<text id="idmigalo" transform="matrix(1 0 0 1 420.2463 553.5321)" class="st4 st2 st5">Migalo</text>
</g>
<g id="TEXT_2_">
<text id="idprime" transform="matrix(1 0 0 1 883.9615 567.5667)" class="st6 st2 st5">Prime</text>
</g>
</svg>
用于插入和更新的书面代码,但无法循环访问结果变量
foreach(结果为var item1)
给出错误作为“严重性代码描述项目文件行抑制状态 错误CS1579 foreach语句无法对类型为'Svg'的变量进行操作,因为'Svg'不包含'GetEnumerator'的公共实例定义SVGXMLToJsonApp C:\ Users \ iqra \ Desktop \ SVGXMLToJsonApp \ SVGXMLToJsonApp \ Program.cs 58 Active“ >
尝试了您提供的文章,但由于方法简单而无法将其插入数据库中。 让我解释一下如何在数据库中插入数据 以下是我在数据库中用于存储如下数据的列
1st Row data:- Rect_Id (idrect3), x(251.4) ,y(-0.3) ,rect_class(st0) ,height(300.2) ,weight(866) ,text(Nano Tech) ,transform(matrix(1 0 0 1 515.7997 166.1773)), Text_Id(idnano), text_class(st1 st2 st3)
2nd Row data:- Rect_Id (idrect2), x(248.6) ,y(366.5) ,rect_class(st0) ,height(400.2) ,weight(500.3) ,text(Migalo) ,transform(matrix(1 0 0 1 420.2463 553.5321)), Text_Id(idmigalo), text_class(st4 st2 st5)
3rd Row data:- Rect_Id (idrect1), x(811.4) ,y(364.2) ,rect_class(st0) ,height(404.1) ,weight(300.2) ,text(Prime) ,transform(matrix(1 0 0 1 883.9615 567.5667)), Text_Id(idprime), text_class(st6 st2 st5)
答案 0 :(得分:1)
首先,就像@Archlight所说的那样,这是一个很好的参考文档,对于任何需要此文档的人,我通常最终都会将该网站用于所有xml相关项目
XML2CS它将您的xml转换为易于集成和使用的xml项目
在OP的情况下,这对于他的需求将是完美的
这是该网站的示例输出,这是您如何在应用程序中使用它的方法
/*
Licensed under the Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0
*/
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="style", Namespace="http://www.w3.org/2000/svg")]
public class Style {
[XmlAttribute(AttributeName="type")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="rect", Namespace="http://www.w3.org/2000/svg")]
public class Rect {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="x")]
public string X { get; set; }
[XmlAttribute(AttributeName="y")]
public string Y { get; set; }
[XmlAttribute(AttributeName="class")]
public string Class { get; set; }
[XmlAttribute(AttributeName="width")]
public string Width { get; set; }
[XmlAttribute(AttributeName="height")]
public string Height { get; set; }
}
[XmlRoot(ElementName="g", Namespace="http://www.w3.org/2000/svg")]
public class G {
[XmlElement(ElementName="rect", Namespace="http://www.w3.org/2000/svg")]
public Rect Rect { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlElement(ElementName="text", Namespace="http://www.w3.org/2000/svg")]
public Text Text { get; set; }
}
[XmlRoot(ElementName="text", Namespace="http://www.w3.org/2000/svg")]
public class Text {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="transform")]
public string Transform { get; set; }
[XmlAttribute(AttributeName="class")]
public string Class { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="svg", Namespace="http://www.w3.org/2000/svg")]
public class Svg {
[XmlElement(ElementName="style", Namespace="http://www.w3.org/2000/svg")]
public Style Style { get; set; }
[XmlAttribute(AttributeName="style")]
public string _Style { get; set; }
[XmlElement(ElementName="g", Namespace="http://www.w3.org/2000/svg")]
public List<G> G { get; set; }
[XmlAttribute(AttributeName="version")]
public string Version { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName="xlink", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xlink { get; set; }
[XmlAttribute(AttributeName="x")]
public string X { get; set; }
[XmlAttribute(AttributeName="y")]
public string Y { get; set; }
[XmlAttribute(AttributeName="viewBox")]
public string ViewBox { get; set; }
[XmlAttribute(AttributeName="space", Namespace="http://www.w3.org/XML/1998/namespace")]
public string Space { get; set; }
}
}
您可以根据需要从此处读取文件,我在下面添加了一个流示例
public static Svg LoadSVG(Stream SVGFile)
{
XmlSerializer serializer = new XmlSerializer(typeof(Svg));
using (TextReader reader = new StreamReader(SVGFile))
{
Svg result = (Svg)serializer.Deserialize(reader);
return result;
}
}
在这里您可以根据需要调用属性
var itemid = svg.Id;