【问题标题】:how to add attributes to xml using xml serializer如何使用 xml 序列化程序向 xml 添加属性
【发布时间】:2016-02-27 05:59:00
【问题描述】:

我有以下代码将保存到 XML 文件,

   private bool CreateAutoGeneratedReportsXML(string ReportName, int ReportId, string ConnectionString, string ReportBQuery, string ReportColName,string starttime,string endtime,string dailytime,bool daily,bool weekly,bool monthly,bool yearly)
    {
        string dir = "C:\\ReportManager\\";
        string fname="AutoReport.xml";
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }

        List<AutoReportXML> objAutoReportXML = new List<AutoReportXML>();

        XmlSerializer objSerializer = new XmlSerializer(typeof(List<AutoReportXML>));
        if(!File.Exists(dir+fname))
        {
            AutoReportXML objx = new AutoReportXML();


            objAutoReportXML.Add(new AutoReportXML() { ReportName = ReportName, ReportID = ReportId, ConnectionString = ConnectionString,
                ReportBQuery = ReportBQuery, ReportColumnName = ReportColName, StartTime = starttime,
                EndTime = endtime, DailyTime = dailytime, Daily = daily, Weekly = weekly, Monthly = monthly, Yearly = yearly });

            using(FileStream fs=new FileStream(dir+fname,FileMode.Create,FileAccess.Write))
            {
                objSerializer.Serialize(fs, objAutoReportXML);
                fs.Close();
            }
        }
        else{
            using (FileStream fs = new FileStream(dir + fname, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {

                objAutoReportXML=objSerializer.Deserialize(fs) as List<AutoReportXML>;

                if (!objAutoReportXML.Any(x => x.ReportName.Contains(ReportName)))
                {
                    AutoReportXML objx=new AutoReportXML();
                    XElement x;
                    if (fs.Position > 0)
                    {
                        fs.Position = 0;
                        x = XElement.Load(fs);
                        x = new XElement("ArrayOfAutoReportXML",                                
                        new XAttribute("ReportName", ReportName),
                        new XAttribute("ReportID", ReportId),
                        new XAttribute("ConnectionString", ConnectionString),
                        new XAttribute("ReportBQuery", ReportBQuery),
                        new XAttribute("ReportColumnName", ReportColName),
                        new XAttribute("StartTime",starttime),
                        new XAttribute("EndTime",endtime),
                        new XAttribute("DailyTime",dailytime),
                        new XAttribute("Daily",daily),
                        new XAttribute("objx.Weekly",weekly),
                        new XAttribute("Monthly",monthly),
                        new XAttribute("Yearly",yearly));


                    x.Save(fs);
                    }



                }
                else {

                }
            }
            }
        return true;
    }

以上代码的输出如下:

 <?xml version="1.0" encoding="utf-8"?>
<ArrayOfAutoReportXML ReportName="tff" ReportID="17" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t0.testid, t0.pt500, t0.pt600, t0.cdt  FROM sampletest t0 WHERE  t0.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="12:46:50" EndTime="12:46:50" DailyTime="12:46:50" Daily="false" objx.Weekly="false" Monthly="false" Yearly="false" /><?xml version="1.0"?>
<ArrayOfAutoReportXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

我希望输出如下所示,添加属性而不是创建子节点:

<?xml version="1.0"?>
<ArrayOfAutoReportXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AutoReportXML ReportName="hourslyshifta" ReportID="34" StartTime="10:00:00" EndTime="17:00:00" ... >
<AutoReportXML ReportName="hourslyshifta" ReportID="34" StartTime="10:00:00" EndTime="17:00:00" ... >
<AutoReportXML ReportName="somename" ReportID="34" StartTime="10:00:00" EndTime="17:00:00" ... >
<AutoReportXML ReportName="othername" ReportID="34" StartTime="10:00:00" EndTime="17:00:00" ... >
<AutoReportXML ReportName="firstname" ReportID="34" StartTime="10:00:00" EndTime="17:00:00" ... >
<AutoReportXML ReportName="quickreport" ReportID="34" StartTime="10:00:00" EndTime="17:00:00" ... >

</ArrayOfAutoReportXML>

如何更改我的代码以添加属性而不是创建子节点。

【问题讨论】:

    标签: c# xml xml-serialization xmlserializer xml-attribute


    【解决方案1】:

    你可以使用:

    XElement x;
    XAttribute attribute = new XAttribute("AttributeName", object);
    x.Add(attribute);
    

    或者:

    XElement x = new XElement("AutoReportXML",
        new XAttribute("ReportName", "somename"),
        new XAttribute("ReportID", 34)
        /* , add more here */);
    

    【讨论】:

    • 谢谢,我又收到一个错误 每次创建新节点时都会添加此行,您能帮我解释一下吗
    • 你能更新你的新代码吗?我只能检查我是否看到代码。
    • 我认为如果(!file.exists)部分代码需要更新。我已经更新了代码
    • 问题是你使用List&lt;AutoReportXML&gt;。您必须创建一个root XML node,然后向其中添加AutoReportXML 元素。
    猜你喜欢
    • 2010-11-03
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 2013-07-29
    相关资源
    最近更新 更多