从SQL解析XML时出现问题 [英] Problem parsing XML from SQL

查看:107
本文介绍了从SQL解析XML时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中读取一些XML.我遇到了一些问题.当我尝试使用XDocument.Parse(xml)时,它将全部作为一个元素读取. NextNode和PreviousNode均为null.以下是我从数据库中收到的XML:

I am trying to read in some XML from my database. I am running in to a bit of a problem. When I try use XDocument.Parse(xml) it is reading it all as one element. NextNode and PreviousNode are both null. Below is the XML I receive from the database:

<extradata><client><weburl>http://wwww.mywebaddress.com</weburl><subscriptions><usermanagement allowed="true" /><formdesign allowed="true" /><formview allowed="true" /></subscriptions><mobileconfig><gps allowed="false" /><photoattach allowed="true" /><barcode allowed="false" /><verification allowed="false" /></mobileconfig><pcrouterconfig><printondemand allowed="true" /><integration allowed="false" /><printany allowed="false" /></pcrouterconfig></client></extradata>



当我得到一个错误时,我得到的错误是没有根节点?任何帮助都将非常有用.



The error I get when I do get an error is that there is no root node? Any help would be great.

推荐答案

以下是示例XML解析器方法,如下所示:

Here is the sample XML parser method as:

void ParseURL(string strUrl)
{
    try
    {
        XmlTextReader reader = new XmlTextReader(strUrl);
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
            case XmlNodeType.Element:
                Hashtable attributes = new Hashtable();
                string strURI= reader.NamespaceURI;
                string strName= reader.Name;
                if (reader.HasAttributes)
                {
                    for (int i = 0; i < reader.AttributeCount; i++)
                    {
                        reader.MoveToAttribute(i);
                        attributes.Add(reader.Name,reader.Value);
                    }
                }
                StartElement(strURI,strName,strName,attributes);
            break;
             //
             //you can handle other cases here
             //
 
             //case XmlNodeType.EndElement:
             // Todo
             //case XmlNodeType.Text:
             // Todo
            default:
            break;
        }
    }
    catch (XmlException e)
    {
        Console.WriteLine("error occured: " + e.Message);
    }
}


使用以下示例代码:


use this sample code:


using System.Xml;
using System.Data.SqlClient;
class Example
{
static void Main()
{
SqlConnection c=new SqlConnection();
c.ConnectionString="*******************************";
c.Open();
SqlDataAdapter adptr=new SqlDataAdapter("Select * from your_table_name",c);
DataSet ds=new DataSet();
adptr.Fill(ds,"your_table_name");
ds.WriteXml("C:\\your_table_name.xml",XmlWriteMode.DiffGram);
c.Close();
}
}



我希望这段代码一定会对您有所帮助....



i hope this code will surely help you....


这篇关于从SQL解析XML时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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