从动态数据集生成XML

时间:2012-01-18 06:27:25

标签: asp.net xml c#-3.0

我在c#中有一个ASP.net应用程序,我有一个数据集,我可以使用以下简单的代码块转移到xml:

string result;
using (StringWriter sw = new StringWriter())
{
ds.WriteXml(sw);
result = sw.ToString();
}

生成如下所示的xml块:

<NewDataSet>
<ImageInfo>
<ImageID>1</ImageID>
<ImageName>untitled.bmp</ImageName>
<ImagePath>Some Path</ImagePath>
<Width>800</Width>
<Height>190</Height>
<TileSize>256</TileSize>
<Overlap>1</Overlap>
<MimeType>image/jpeg</MimeType>
<Scale />
</ImageInfo>
</NewDataSet>

这显然是一行的示例,数据集中有多行。现在,我不知道如何动态格式化XML块。 谁能指点我这方面的一些好教程? 我希望XML格式如下

<data>
<Columns>
<Column Name= "ImageID"/>
<Column Name= "ImageName"/>
<Column Name= "ImagePath"/>
<Column Name= "Width"/>
<Column Name= "Height"/>
<Column Name= "TileSize"/>
<Column Name= "Overlap"/>
<Column Name= "MimeType"/>
<Column Name= "Scale"/>
</Columns>
<Rows>
<Row>
<Cell>1</Cell>
<Cell>untitled.bmp</Cell>
<Cell>Some Path</Cell>
<Cell>800</Cell>
<Cell>190</Cell>
<Cell>256</Cell>
<Cell>1</Cell>
<Cell>image/jpeg</Cell>
<Cell></Cell>
</Row>
<Row>
.
.
.
</Row>
</Rows>
</data>

任何人都可以指导我如何做到这一点吗?

在我的应用程序中,我事先不会知道数据集的结构。即可能有一个结果数据集,其中我的列数多于示例中描述的列数。我该怎么办呢?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试使用DataTable,然后将该DataTable转换为XML流或文件。

DataTable myDataTable = myDataSet.Tables[0];


myDataTable.WriteXml(..);

你会发现像TextWriter,Stream这样的一大堆重载。最后,您需要修改该xml以添加除DataTable之外的节点。

答案 1 :(得分:0)

这可以解决这个问题。代码标准可以更好,但现在您可以使用以下内容:

XmlDocument doc = new XmlDocument();
        XmlDocument Outputdoc = new XmlDocument();
        doc.LoadXml("<NewDataSet> <ImageInfo> <ImageID>1</ImageID> <ImageName>untitled.bmp</ImageName> <ImagePath>Some Path</ImagePath> <Width>800</Width> <Height>190</Height> <TileSize>256</TileSize> <Overlap>1</Overlap> <MimeType>image/jpeg</MimeType> <Scale /> <ImageID>1</ImageID> <ImageName>untitled.bmp</ImageName> <ImagePath>Some Path</ImagePath> <Width>800</Width> <Height>190</Height> <TileSize>256</TileSize> <Overlap>1</Overlap> <MimeType>image/jpeg</MimeType> <Scale /></ImageInfo> </NewDataSet> ");
        XmlNode node = doc.SelectSingleNode("/NewDataSet/ImageInfo");
        XmlNodeList nodes = node.ChildNodes;
        XmlDocument newdoc = null;
        StringBuilder sb = new StringBuilder("<data><Columns>");
        string lastNode=string.Empty;
        string firstNode = string.Empty;
        foreach (XmlNode n in node.ChildNodes)
        {
            if(n.Name.Equals(firstNode))
                break;
            if (string.IsNullOrEmpty(firstNode))
                firstNode = n.Name;

            newdoc = new XmlDocument();
            XmlNode newNode = newdoc.CreateElement("Column");
            XmlAttribute newAttribute = newdoc.CreateAttribute("Name");
            newAttribute.Value = n.Name;
            newNode.Attributes.Append(newAttribute);
            newdoc.AppendChild(newNode);
            sb.Append(newdoc.InnerXml);
            lastNode=n.Name;
        }
        sb.Append("</Columns><Rows><Row>");
        foreach (XmlNode xnode in node.ChildNodes)
        {
            XmlDocument newdoc1 = new XmlDocument();
            XmlNode newNode = newdoc1.CreateElement("Cell");
            newNode.InnerText = xnode.InnerText;
            newdoc1.AppendChild(newNode);
            sb.Append(newdoc1.InnerXml);
            if (xnode.Name.Equals(lastNode))
                sb.Append("</Row>");
        }
        sb.Append("</Rows></data>");

这是完成任务的一种非常快捷的方法。您可以自由地对代码进行标准化。现在看一下你需要的输出:

<data>
<Columns>
    <Column Name="ImageID" />
    <Column Name="ImageName" />
    <Column Name="ImagePath" />
    <Column Name="Width" />
    <Column Name="Height" />
    <Column Name="TileSize" />
    <Column Name="Overlap" />
    <Column Name="MimeType" />
    <Column Name="Scale" />
</Columns>
<Rows>
    <Row>
        <Cell>1</Cell>
        <Cell>untitled.bmp</Cell>
        <Cell>SomePath</Cell>
        <Cell>800</Cell>
        <Cell>190</Cell>
        <Cell>256</Cell>
        <Cell>1</Cell>
        <Cell>image/jpeg</Cell>
        <Cell></Cell>
    </Row>
        <Cell>1</Cell>
        <Cell>untitled.bmp</Cell>
        <Cell>Some Path</Cell>
        <Cell>800</Cell>
        <Cell>190</Cell>
        <Cell>256</Cell>
        <Cell>1</Cell>
        <Cell>image/jpeg</Cell>
        <Cell></Cell>
    </Row>
</Rows>
</data>

注意:我使用相同的数据集合来制作两行数据。