在数据库中插入 XML 数据 [英] Insert XML data in database

查看:39
本文介绍了在数据库中插入 XML 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我将 SVG(XML) 文件转换为 C# 对象后,我想在数据库中插入 XML 数据但无法插入

下面是代码

 试试{string uri = @"C:\Users\iqra\Desktop\SVGXMLToJsonApp\SVGXMLToJsonApp\File\3rect.svg";XmlSerializer 序列化器 = new XmlSerializer(typeof(Svg));使用 (TextReader reader = new StreamReader(uri)){Svg 结果 = (Svg)serializer.Deserialize(reader);//Console.WriteLine(result);列表<矩形>List2 = new List();string con = "Data Source=.;Initial Catalog=FloorPlan;Integrated Security=True";使用 (SqlConnection conn = new SqlConnection(con)){string query = "select * from Stand_Details";SqlCommand cmd1 = new SqlCommand(query, conn);SqlDataReader 博士;conn.Open();dr = cmd1.ExecuteReader();而 (dr.Read()){矩形 newItem = 新矩形();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();}}列表<矩形>第三列表 = 新列表();foreach(结果为var item1){bool isMatch = false;foreach(List2 中的 var item2){//如果(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("记录存在");}别的{尝试{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";使用 (SqlCommand cmd = new SqlCommand(update, cnn)){//遍历并获取参数值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;}}捕获(异常前){Console.WriteLine(ex);}休息;}}如果 (!isMatch){第三个列表.添加(项目1);}}foreach(第三个列表中的矩形 p){尝试{//在此处创建与数据库的 SQL 连接使用 (SqlConnection cnn = new SqlConnection(con)){//打开你的连接cnn.Open();//这里修改表名string sql = "设置 ANSI_WARNINGS 关闭;插入 Stand_Details(Id,Class, Width, Height,x,y) VALUES (@Id,@Class, @Width, @Height,@x,@y)";//创建命令和参数对象.使用 (SqlCommand cmd = new SqlCommand(sql, cnn)){//遍历并获取参数值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);//执行查询cmd.ExecuteNonQuery();}}}捕获(异常前){字符串 errorMsg = ex.Message.ToString();}}}}捕获(异常前){Console.WriteLine(ex);}

下面是我的课

 [XmlRoot(ElementName = "style", Namespace = "http://www.w3.org/2000/svg")]公开课风格{[XmlAttribute(AttributeName = "type")]公共字符串类型{获取;放;}[XML文本]公共字符串文本 { 获取;放;}}[XmlRoot(ElementName = "rect", Namespace = "http://www.w3.org/2000/svg")]公共类矩形{[XmlAttribute(AttributeName = "id")]公共字符串 ID { 获取;放;}[XmlAttribute(AttributeName = "x")]公共字符串 X { 得到;放;}[XmlAttribute(AttributeName = "y")]公共字符串 Y { 得到;放;}[XmlAttribute(AttributeName = "class")]公共字符串类{获取;放;}[XmlAttribute(AttributeName = "width")]公共字符串宽度{获取;放;}[XmlAttribute(AttributeName = "height")]公共字符串高度{得到;放;}}[XmlRoot(ElementName = "g", Namespace = "http://www.w3.org/2000/svg")]公开课 G{[XmlElement(ElementName = "rect", Namespace = "http://www.w3.org/2000/svg")]公共矩形矩形{获取;放;}[XmlAttribute(AttributeName = "id")]公共字符串 ID { 获取;放;}[XmlElement(ElementName = "text", Namespace = "http://www.w3.org/2000/svg")]公共文本文本{得到;放;}}[XmlRoot(ElementName = "text", Namespace = "http://www.w3.org/2000/svg")]公共课文{[XmlAttribute(AttributeName = "id")]公共字符串 ID { 获取;放;}[XmlAttribute(AttributeName = "transform")]公共字符串转换 { 获取;放;}[XmlAttribute(AttributeName = "class")]公共字符串类{获取;放;}[XML文本]公共字符串 Text1 { 获取;放;}}[XmlRoot(ElementName = "svg", Namespace = "http://www.w3.org/2000/svg")]公共类 Svg{[XmlElement(ElementName = "style", Namespace = "http://www.w3.org/2000/svg")]公共风格风格{得到;放;}[XmlAttribute(AttributeName = "style")]公共字符串 _Style { 获取;放;}[XmlElement(ElementName = "g", Namespace = "http://www.w3.org/2000/svg")]公共列表<G>G{得到;放;}[XmlAttribute(AttributeName = "version")]公共字符串版本 { 获取;放;}[XmlAttribute(AttributeName = "id")]公共字符串 ID { 获取;放;}[XmlAttribute(AttributeName = "xmlns")]公共字符串 Xmlns { 获取;放;}[XmlAttribute(AttributeName = "xlink", Namespace = "http://www.w3.org/2000/xmlns/")]公共字符串 Xlink { 获取;放;}[XmlAttribute(AttributeName = "x")]公共字符串 X { 得到;放;}[XmlAttribute(AttributeName = "y")]公共字符串 Y { 得到;放;}[XmlAttribute(AttributeName = "viewBox")]公共字符串 ViewBox { 获取;放;}[XmlAttribute(AttributeName = "space", Namespace = "http://www.w3.org/XML/1998/namespace")]公共字符串空间{获取;放;}}

以下是我想插入到数据库中的 XML 数据

 <?xml version="1.0" encoding="utf-8"?><!-- 生成器:Adobe Illustrator 22.0.0,SVG 导出插件.SVG 版本: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="保留"><style type="text/css">.st0{fill:none;stroke:#000000;stroke-width:0.7087;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}.st1{填充:#105689;}.st2{font-family:'ArialMT';}.st3{字体大小:56.5499px;}.st4{填充:#4554A5;}.st5{字体大小:56.5574px;}.st6{填充:#2776BB;}</风格><g id="LWPOLYLINE"><rect id="idrect3" x="251.4" y="-0.3" class="st0" width="866" height="300.2"/><g id="LWPOLYLINE_1_"><rect id="idrect2" x="248.6" y="366.5" class="st0" width="500.3" height="400.2"/><g id="LWPOLYLINE_2_"><rect id="idrect1" x="811.4" y="364.2" class="st0" width="300.2" height="404.1"/><g id="文本"><text id="idnano" transform="matrix(1 0 0 1 515.7997 166.1773)" class="st1 st2 st3">Nano Tech</text><g id="TEXT_1_"><text id="idmigalo" transform="matrix(1 0 0 1 420.2463 553.5321)" class="st4 st2 st5">Migalo</text><g id="TEXT_2_"><text id="idprime" transform="matrix(1 0 0 1 883.9615 567.5667)" class="st6 st2 st5">Prime</text></svg>

用于插入和更新但无法在循环中访问结果变量的书面代码

foreach (var item1 in result)

给出错误为严重性代码描述项目文件行抑制状态错误 CS1579 foreach 语句无法对类型为 'Svg' 的变量进行操作,因为 'Svg' 不包含 'GetEnumerator' SVGXMLToJsonApp C:\Users\iqra\Desktop\SVGXMLToJsonApp\SVGXMLToJsonApp\Program.cs 58 Active"

尝试了您提供的文章,但无法将其插入数据库,因为方法很简单.让我解释一下我想如何在数据库中插入数据以下是我在数据库中存储数据的列,如下所示

第一行数据:- Rect_Id (idrect3), x(251.4) ,y(-0.3) ,rect_class(st0) ,height(300.2) ,weight(866) ,text(Nano Tech) ,transform(矩阵(1 0 0 1 515.7997 166.1773)),Text_Id(idnano),text_class(s​​t1 st2 st3)第二行数据:- 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.2345))), Text_Id(idmigalo), text_class(st4 st2 st5)第三行数据:- 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 88567656155))), Text_Id(idprime), text_class(st6 st2 st5)

解决方案

首先,正如@Archlight 所说,这是一个很好的参考文档,对于需要此文档的任何其他人,我通常最终使用此站点处理所有与 xml 相关的项目

XML2CS 它将您的 xml 转换为易于集成和可用的 xml 项目

在 OP 的情况下,这将非常适合他的需要

这里是网站的示例输出,这里是你如何在你的应用程序中使用它

/*在 Apache 许可下获得许可,版本 2.0http://www.apache.org/licenses/LICENSE-2.0*/使用系统;使用 System.Xml.Serialization;使用 System.Collections.Generic;命名空间 Xml2CSharp{[XmlRoot(ElementName="style", Namespace="http://www.w3.org/2000/svg")]公共类样式{[XmlAttribute(AttributeName="type")]公共字符串类型{获取;放;}[XML文本]公共字符串文本 { 获取;放;}}[XmlRoot(ElementName="rect", Namespace="http://www.w3.org/2000/svg")]公共类矩形{[XmlAttribute(AttributeName="id")]公共字符串 ID { 获取;放;}[XmlAttribute(AttributeName="x")]公共字符串 X { 得到;放;}[XmlAttribute(AttributeName="y")]公共字符串 Y { 得到;放;}[XmlAttribute(AttributeName="class")]公共字符串类{获取;放;}[XmlAttribute(AttributeName="width")]公共字符串宽度{获取;放;}[XmlAttribute(AttributeName="height")]公共字符串高度{得到;放;}}[XmlRoot(ElementName="g", Namespace="http://www.w3.org/2000/svg")]公共类 G {[XmlElement(ElementName="rect", Namespace="http://www.w3.org/2000/svg")]公共矩形矩形{获取;放;}[XmlAttribute(AttributeName="id")]公共字符串 ID { 获取;放;}[XmlElement(ElementName="text", Namespace="http://www.w3.org/2000/svg")]公共文本文本{得到;放;}}[XmlRoot(ElementName="text", Namespace="http://www.w3.org/2000/svg")]公共类文本{[XmlAttribute(AttributeName="id")]公共字符串 ID { 获取;放;}[XmlAttribute(AttributeName="transform")]公共字符串转换 { 获取;放;}[XmlAttribute(AttributeName="class")]公共字符串类{获取;放;}[XML文本]公共字符串文本 { 获取;放;}}[XmlRoot(ElementName="svg", Namespace="http://www.w3.org/2000/svg")]公共类 Svg {[XmlElement(ElementName="style", Namespace="http://www.w3.org/2000/svg")]公共风格风格{得到;放;}[XmlAttribute(AttributeName="style")]公共字符串 _Style { 获取;放;}[XmlElement(ElementName="g", Namespace="http://www.w3.org/2000/svg")]公共列表<G>G{得到;放;}[XmlAttribute(AttributeName="version")]公共字符串版本 { 获取;放;}[XmlAttribute(AttributeName="id")]公共字符串 ID { 获取;放;}[XmlAttribute(AttributeName="xmlns")]公共字符串 Xmlns { 获取;放;}[XmlAttribute(AttributeName="xlink", Namespace="http://www.w3.org/2000/xmlns/")]公共字符串 Xlink { 获取;放;}[XmlAttribute(AttributeName="x")]公共字符串 X { 得到;放;}[XmlAttribute(AttributeName="y")]公共字符串 Y { 得到;放;}[XmlAttribute(AttributeName="viewBox")]公共字符串 ViewBox { 获取;放;}[XmlAttribute(AttributeName="space", Namespace="http://www.w3.org/XML/1998/namespace")]公共字符串空间{获取;放;}}}

您可以从这里随意读取文件,我在下面添加了一个流示例

public static Svg LoadSVG(Stream SVGFile){XmlSerializer 序列化器 = new XmlSerializer(typeof(Svg));使用 (TextReader reader = new StreamReader(SVGFile)){Svg 结果 = (Svg)serializer.Deserialize(reader);返回结果;}}

从这里您可以根据需要调用属性,例如

var itemid = svg.Id;

In my code i have converted SVG(XML) file into C# objects after converting it i want to insert the XML data in database but is unable to insert

Below is the code

 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);
        }

Below is my class

    [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; }
}

and below is the XML data that i want to insert in database

    <?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>

Written code for insert and update but not able to access result variable in loop

foreach (var item1 in result)

giving error as "Severity Code Description Project File Line Suppression State Error CS1579 foreach statement cannot operate on variables of type 'Svg' because 'Svg' does not contain a public instance definition for 'GetEnumerator' SVGXMLToJsonApp C:\Users\iqra\Desktop\SVGXMLToJsonApp\SVGXMLToJsonApp\Program.cs 58 Active"

Tried the article u have given but not able to insert it in database because the method is simple. Let me explain you how i want to insert data in DB Below is my columns in DB to store data as given below

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)

解决方案

Firstly as @Archlight said thats a great reference document and just for anyone else who ever needs this i usally end up using this site for all xml related items

XML2CS it converts your xml to a easy integrate-able and usable xml item

which in the OP's case would be perfect for what he needs

here is the example output of the site and here is how you can use it in your app

    /* 
    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; }
    }

}

You can read the file as you wish from here i added a stream example below

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;
                }
            }

From here you can call the attributes as you wish for instance

var itemid = svg.Id;

这篇关于在数据库中插入 XML 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆