使用最佳方式编写 XML(Linq To XML 或其他)

发布于 2024-08-24 09:46:08 字数 1116 浏览 6 评论 0原文

我想用以下格式编写我的 xml。我该怎么做?我正在使用 c#

<map borderColor='c5e5b8' fillColor='6a9057' numberSuffix=' Mill.' includeValueInLabels='0' labelSepChar=': ' baseFontSize='9' showFCMenuItem='0'
hoverColor='c2bc23' showTitle='0' type='0' showCanvasBorder='0' bgAlpha='0,0' hoveronEmpty='1' includeNameInLabels='0' showLabels='1'>
<!--toolText='Alaska'imageSave='1' imageSaveURL='Path/FusionChartsSave.aspx or FusionChartsSave.php'-->
<data>
<entity id='AL' value='AL' link="JavaScript:FilterClientProjectList('AL');" fontBold='1' showLabel='0' />
<entity id='AK' value='AK' link="JavaScript:FilterClientProjectList('AK');" fontBold='1' hoverColor='6a9057'/>
<entity id='AZ' value='AZ' link="JavaScript:FilterClientProjectList('AZ');" fontBold='1'/>
</data>

<styles>
<definition>
<style name='MyFirstFontStyle' type='font' face='Verdana' size='11' color='0372AB' bold='1' bgColor='FFFFFF' />
</definition>
<application>
 <apply toObject='Labels' styles='' />
</application>
</styles>
</map>

提前致谢..

I want to write my xml with following format. How can i do it?I am using c#

<map borderColor='c5e5b8' fillColor='6a9057' numberSuffix=' Mill.' includeValueInLabels='0' labelSepChar=': ' baseFontSize='9' showFCMenuItem='0'
hoverColor='c2bc23' showTitle='0' type='0' showCanvasBorder='0' bgAlpha='0,0' hoveronEmpty='1' includeNameInLabels='0' showLabels='1'>
<!--toolText='Alaska'imageSave='1' imageSaveURL='Path/FusionChartsSave.aspx or FusionChartsSave.php'-->
<data>
<entity id='AL' value='AL' link="JavaScript:FilterClientProjectList('AL');" fontBold='1' showLabel='0' />
<entity id='AK' value='AK' link="JavaScript:FilterClientProjectList('AK');" fontBold='1' hoverColor='6a9057'/>
<entity id='AZ' value='AZ' link="JavaScript:FilterClientProjectList('AZ');" fontBold='1'/>
</data>

<styles>
<definition>
<style name='MyFirstFontStyle' type='font' face='Verdana' size='11' color='0372AB' bold='1' bgColor='FFFFFF' />
</definition>
<application>
 <apply toObject='Labels' styles='' />
</application>
</styles>
</map>

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨落画卷 2024-08-31 09:46:08

我将使用 LINQ to SQL(mydatasource 假设为 SQL),然后使用 LINQ to XML,然后使用 XSLT 来获取您正在寻找的确切 XML。

这是一个例子:
我的 XML

<Promotions> 
      <Promotion> 
        <Category>Arts & Entertainment</Category> 
        <Client>Client 1</Client> 
        <ID>2</ID> 
        <Title>Get your Free 2</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Community & Neighborhood</Category> 
        <Client>Client1</Client> 
        <ID>4</ID> 
        <Title>Get your Free 4</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Community & Neighborhood</Category> 
        <Client>Client 1</Client> 
        <ID>5</ID> 
        <Title>Get your Free 5</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Community & Neighborhood</Category> 
        <Client>Client 2</Client> 
        <ID>1</ID> 
        <Title>Get your Free 1</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Education</Category> 
        <Client>Client 3</Client> 
        <ID>3</ID> 
        <Title>Get Your Free 3</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Home & Garden</Category> 
        <Client>Client 4</Client> 
        <ID>6</ID> 
        <Title>Get your Free 6</Title> 
      </Promotion> 
    </Promotions> 

我的代码:

 PromotionsDataContext db = new PromotionsDataContext();
                        //load sql into XML for tree view js control
                        XElement Categories =
                            new XElement("Promotions",
                                from b in db.Promotion_GetPromotions()
                                select new XElement("Promotion",
                                    new XElement("Category", b.CategoryName),
                                       new XElement("Client", b.ClientName),
                                       new XElement("ID", b.ID),
                                       new XElement("Title", b.Title)));

                        XDocument mydoc = new XDocument();
                        mydoc.Add(Categories);

                        try
                        {

                        // Load the style sheet.
                        XslCompiledTransform xslt = new XslCompiledTransform();
                        xslt.Load(@"C:\TransList.xslt");

                        // Execute the transform and output the results to a writer.
                        StringWriter sw = new StringWriter();
                        //XsltSettings mysettings = new XsltSettings();
                        XmlWriterSettings mysettings = new XmlWriterSettings();

                        xslt.Transform(mydoc.CreateReader(), null, sw);

我的 XSLT 文件:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="http://schemas.microsoft.com/ASPNET/20">


  <xsl:output method="html" indent="yes" />

  <xsl:key name="categories" match="Category" use="." />
  <xsl:key name="client" match="Client" use="." />
  <xsl:key name="title" match="Title" use="." />

  <xsl:template match="/">

    <ul id="red" class="treeview-red">
      <xsl:for-each select="/Promotions/Promotion/Category[  
                generate-id(.) = generate-id(key('categories', .)[1])  
                ]">
        <li>
          <span>
            <xsl:value-of select="."/>
            <!--Category-->
          </span>

          <ul>
            <xsl:call-template name="category-client">
              <xsl:with-param name="category" select="."/>
              <!--Client-->
            </xsl:call-template>
          </ul>
        </li>

      </xsl:for-each>
    </ul>

  </xsl:template>

  <xsl:template name="category-client">
    <xsl:param name="category" />
    <xsl:for-each select="/Promotions/Promotion[Category=$category]/Client[  
                generate-id(.) = generate-id(key('client', .)[1]) 
                ]">
      <li>
        <span>
          <xsl:value-of select="."/>
        </span>
        <ul>
          <xsl:call-template name="category-client-title">
            <xsl:with-param name="category" select="$category"/>
            <!--Title-->
            <xsl:with-param name="client" select="."/>
          </xsl:call-template>
        </ul>
      </li>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="category-client-title">
    <xsl:param name="category" />
    <xsl:param name="client" />
    <xsl:for-each select="/Promotions/Promotion[Category=$category]/Title[  
                generate-id(.) = generate-id(key('title', .)[1]) 
                ]">
      <li>
        <span>
          <asp:LinkButton ID ="LinkButton{../ID}" runat="server" OnClick="LinkClicked" Text="{.}">
          </asp:LinkButton>
        </span>
      </li>

    </xsl:for-each>

  </xsl:template>

</xsl:stylesheet>

以下是我在 C# 中使用 XSLT 时发现的一些内容:

我在这里做错了什么,使用 C# 处理 XSLT 时遇到问题

如何在 XSLT 中通过标记名称检索同级?

XSLT 渲染 >且<对于><我该如何解决这个问题?

I would use LINQ to SQL(mydatasource assumed SQL) then LINQ to XML then XSLT to get the exact XML you are looking for.

Here is an example:
My XML

<Promotions> 
      <Promotion> 
        <Category>Arts & Entertainment</Category> 
        <Client>Client 1</Client> 
        <ID>2</ID> 
        <Title>Get your Free 2</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Community & Neighborhood</Category> 
        <Client>Client1</Client> 
        <ID>4</ID> 
        <Title>Get your Free 4</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Community & Neighborhood</Category> 
        <Client>Client 1</Client> 
        <ID>5</ID> 
        <Title>Get your Free 5</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Community & Neighborhood</Category> 
        <Client>Client 2</Client> 
        <ID>1</ID> 
        <Title>Get your Free 1</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Education</Category> 
        <Client>Client 3</Client> 
        <ID>3</ID> 
        <Title>Get Your Free 3</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Home & Garden</Category> 
        <Client>Client 4</Client> 
        <ID>6</ID> 
        <Title>Get your Free 6</Title> 
      </Promotion> 
    </Promotions> 

My code:

 PromotionsDataContext db = new PromotionsDataContext();
                        //load sql into XML for tree view js control
                        XElement Categories =
                            new XElement("Promotions",
                                from b in db.Promotion_GetPromotions()
                                select new XElement("Promotion",
                                    new XElement("Category", b.CategoryName),
                                       new XElement("Client", b.ClientName),
                                       new XElement("ID", b.ID),
                                       new XElement("Title", b.Title)));

                        XDocument mydoc = new XDocument();
                        mydoc.Add(Categories);

                        try
                        {

                        // Load the style sheet.
                        XslCompiledTransform xslt = new XslCompiledTransform();
                        xslt.Load(@"C:\TransList.xslt");

                        // Execute the transform and output the results to a writer.
                        StringWriter sw = new StringWriter();
                        //XsltSettings mysettings = new XsltSettings();
                        XmlWriterSettings mysettings = new XmlWriterSettings();

                        xslt.Transform(mydoc.CreateReader(), null, sw);

My XSLT file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="http://schemas.microsoft.com/ASPNET/20">


  <xsl:output method="html" indent="yes" />

  <xsl:key name="categories" match="Category" use="." />
  <xsl:key name="client" match="Client" use="." />
  <xsl:key name="title" match="Title" use="." />

  <xsl:template match="/">

    <ul id="red" class="treeview-red">
      <xsl:for-each select="/Promotions/Promotion/Category[  
                generate-id(.) = generate-id(key('categories', .)[1])  
                ]">
        <li>
          <span>
            <xsl:value-of select="."/>
            <!--Category-->
          </span>

          <ul>
            <xsl:call-template name="category-client">
              <xsl:with-param name="category" select="."/>
              <!--Client-->
            </xsl:call-template>
          </ul>
        </li>

      </xsl:for-each>
    </ul>

  </xsl:template>

  <xsl:template name="category-client">
    <xsl:param name="category" />
    <xsl:for-each select="/Promotions/Promotion[Category=$category]/Client[  
                generate-id(.) = generate-id(key('client', .)[1]) 
                ]">
      <li>
        <span>
          <xsl:value-of select="."/>
        </span>
        <ul>
          <xsl:call-template name="category-client-title">
            <xsl:with-param name="category" select="$category"/>
            <!--Title-->
            <xsl:with-param name="client" select="."/>
          </xsl:call-template>
        </ul>
      </li>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="category-client-title">
    <xsl:param name="category" />
    <xsl:param name="client" />
    <xsl:for-each select="/Promotions/Promotion[Category=$category]/Title[  
                generate-id(.) = generate-id(key('title', .)[1]) 
                ]">
      <li>
        <span>
          <asp:LinkButton ID ="LinkButton{../ID}" runat="server" OnClick="LinkClicked" Text="{.}">
          </asp:LinkButton>
        </span>
      </li>

    </xsl:for-each>

  </xsl:template>

</xsl:stylesheet>

Here are some things I found when working with XSLT in C#:

What am I doing wrong here, having issues with XSLT using C#

How do I retrieve a sibling by tag name in XSLT?

XSLT renders > and < for >< how to do I get around this?

凶凌 2024-08-31 09:46:08

如果我有一个需要大量使用 Xml 的项目,我总是创建一个 VB 项目。您可以像这样声明示例 xml:

Dim sampleXml = 
<Promotions> 
      <Promotion> 
        <Category>Arts & Entertainment</Category> 
        <Client>Client 1</Client> 
        <ID>2</ID> 
        <Title>Get your Free 2</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Client 1</Category> 
        <Client>Artsquest</Client> 
        <ID>4</ID> 
        <Title>Get your Free 4</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Client 1</Category> 
        <Client>Artsquest</Client> 
        <ID>5</ID> 
        <Title>Get your Free 5</Title> 
      </Promotion> 
      (....)

并检索像这样的项目:

Dim firstPromotion = sampleXml.Promotions.Promotion(0)

或类似的内容。

即使我有一个 C# 项目,我也会创建一个 VB dll 项目来处理 xml。它比您必须在 C# 中执行的 newXElement 操作干净得多。

If I have a project which intensively works with Xml, I always create a VB Project. You could declare the sample xml like:

Dim sampleXml = 
<Promotions> 
      <Promotion> 
        <Category>Arts & Entertainment</Category> 
        <Client>Client 1</Client> 
        <ID>2</ID> 
        <Title>Get your Free 2</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Client 1</Category> 
        <Client>Artsquest</Client> 
        <ID>4</ID> 
        <Title>Get your Free 4</Title> 
      </Promotion> 
      <Promotion> 
        <Category>Client 1</Category> 
        <Client>Artsquest</Client> 
        <ID>5</ID> 
        <Title>Get your Free 5</Title> 
      </Promotion> 
      (....)

and retrieve an item like:

Dim firstPromotion = sampleXml.Promotions.Promotion(0)

or something similar.

Even if I have a C# project, I create an VB dll-project to work with xml. It is much cleaner than the newXElement stuff you have to do in C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文