I am new to web programming and Visual Web Developer. I have made a page that prompts the user for input and then this input replaces the prompts using InnerHTML accesses. Thus the table can only be edited the first time it is displayed. It is the final, edited version of the HTML that I want others to be able to access. So I need a way to write the edits out to an XML file. I understand that this can be done with LINQ but I haven't figured out how to do it.
Any advice is appreciated.
Regards.
Here's an example I found online at: http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx. You should be able to figure it out if you compare the LINQ to XML code with the file output.
Code:
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("rss",
new XAttribute("version", "2.0"),
new XElement ("channel",
new XElement("title", "RSS Channel Title"),
new XElement("description", "RSS Channel Description."),
new XElement("link", "http://aspiring-technology.com"),
new XElement("item",
new XElement("title", "First article title"),
new XElement("description", "First Article Description"),
new XElement("pubDate", DateTime.Now.ToUniversalTime()),
new XElement("guid", Guid.NewGuid())),
new XElement("item",
new XElement("title", "Second article title"),
new XElement("description", "Second Article Description"),
new XElement("pubDate", DateTime.Now.ToUniversalTime()),
new XElement("guid", Guid.NewGuid()))
)
)
);
doc.Save(#"c:\sample.xml");
file:
<rss version="2.0">
<channel>
<title>RSS Channel Title</title>
<description>RSS Channel Description.</description>
<link>http://aspiring-technology.com</link>
<item>
<title>First article title</title>
<description>First Article Description</description>
<pubDate>2006-12-05T20:53:53.53125</pubDate>
<guid>ff7bbf19-9155-4773-913c-767bcbf09904</guid>
</item>
<item>
<title>Second article title</title>
<description>Second Article Description</description>
<pubDate>2006-12-05T20:53:53.5625</pubDate>
<guid>8a3fd5e8-b99f-49fe-8a43-7fb62d80c18c</guid>
</item>
</channel>
</rss>
Related
i am very new to this. Hope someone could help me suggest how to improve the code.
I have two tables where i need to get the SQL data and ouput it into XML format. I am using LINQ method. Below how the code looks like.
#region Database XML Methods
private static void CreateDatabaseXml(string path)
{
tbchrDataContext db = new tbchrDataContext();
XDocument doc = new XDocument(
// XML Declaration
new XDeclaration("1.0", "utf-8", "yes"),
// XML Root element to 3rd in nest
new XElement(ns + "WMS",
new XElement(ns + "Order",
new XElement(ns + "Header", from a in db.T_ORDER_DETAILs
select new XElement(ns + "RARefNum", a.RARefNum),
new XElement (ns + "WMSCategory", from b in db.T_ORDER_HEADERs select b.Customer),
new XElement (ns + "CustomerID", from a in db.T_ORDER_DETAILs select a.SupplierName)))) );
#endregion
doc.Save(path);
}
And below how is the output of XML looks like.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<WMS xmlns="http://blog.cripperz.sg">
<Order>
<Header>
<RARefNum>RASO000001</RARefNum>
<RARefNum>RASO000001</RARefNum>
<WMSCategory>ESSVMI</WMSCategory>
<CustomerID>nVidianVidia</CustomerID>
</Header>
</Order>
</WMS>
Ultimately i wanted to achieve the below XML, some of the data grab from SQL is from two separate tables in one XML nest / element.
<?xml version="1.0" encoding="utf-8"?>
<WMS>
<Order>
<Header>
<RARefNum>RASO000001</RARefNum>
<WMSCategory>ESSVMI</WMSCategory>
<CustomerID>nVidia</CustomerID>
<CreationDate>2013-12-02 06:29:50</CreationDate>
<OrderDate>2013-12-02 06:29:50</OrderDate>
<ExpectedShippedDate>2013-12-02 06:29:50</ExpectedShippedDate>
<LastShippedDate>2013-12-02 06:29:50</LastShippedDate>
<CustomerOrderReference>nVidia9338</CustomerOrderReference>
<CustomerShipmentNo>81475721</CustomerShipmentNo>
<CustomerSONo>SO982733</CustomerSONo>
<CustomerInvoiceNo>INV987373</CustomerInvoiceNo>
<CustomerReference1>nVidia 1</CustomerReference1>
<CustomerReference2/>
<WMSReference1>Emp 1</WMSReference1>
<WMSReference2>Emp 2</WMSReference2>
<ShipmentNo>IWU997872</ShipmentNo>
<DocumentNo>KK98764394</DocumentNo>
<Transportation>
<Mode>Freight</Mode>
<VehicleType/>
</Transportation>
<Carrier>
<ID>Fedex</ID>
<Name>Fedex SG</Name>
<Address>Changi Singapore</Address>
<Country/>
<PostalCode/>
<Contact>
<Sequence/>
<Person/>
<Email/>
<DID/>
<Handphone/>
</Contact>
</Carrier>
<Consignee>
<ID>ABC</ID>
<Name>ABC Corp</Name>
<Address>Jurong West, Singapore</Address>
<Country/>
<PostalCode/>
<Contact>
<Sequence/>
<Person/>
<Email/>
<DID/>
<Handphone/>
</Contact>
</Consignee>
<Containers/>
</Header>
<Details>
<Detail>
<LineNo>1</LineNo>
<SKU>SKU0001</SKU>
<SKUDescription>SKU 0001</SKUDescription>
<Package>50</Package>
<OrderedQty>600.000</OrderedQty>
<PickedQty>600.000</PickedQty>
<PickedDate>2013-12-02 06:35:09</PickedDate>
<ShippedQty>600.000</ShippedQty>
<ShippedDate>2013-12-02 06:35:09</ShippedDate>
<ManufactoryDate>2013-12-02 06:35:09</ManufactoryDate>
<ExpiryDate>2014-12-02 06:35:09</ExpiryDate>
<FIFODate>2013-06-02 06:35:09</FIFODate>
<CustomerLotRef1>nVidia 2093</CustomerLotRef1>
<CustomerLotRef2>nVidia 2099</CustomerLotRef2>
<LineReference1>10</LineReference1>
</Detail>
<Detail>
<LineNo>2</LineNo>
<SKU>SKU0002</SKU>
<SKUDescription>SKU 0002</SKUDescription>
<Package>50</Package>
<OrderedQty>100.000</OrderedQty>
<PickedQty>100.000</PickedQty>
<PickedDate>2013-12-02 06:35:09</PickedDate>
<ShippedQty>100.000</ShippedQty>
<ShippedDate>2013-12-02 06:35:09</ShippedDate>
<ManufactoryDate>2013-12-02 06:35:09</ManufactoryDate>
<ExpiryDate>2014-12-02 06:35:09</ExpiryDate>
<FIFODate>2013-06-02 06:35:09</FIFODate>
<CustomerLotRef1>nVidia 2193</CustomerLotRef1>
<CustomerLotRef2>nVidia 2199</CustomerLotRef2>
<LineReference1>10</LineReference1>
</Detail>
</Details>
</Order>
</WMS>
Is there a better way to code it?
In sql server support lot of xml format outputs.
This query returns a xml document from two tables. Maybe you are using
linq, then after completing your sql query, you execute the query from
linq.
select table1.column1, table2.column2 from table1 inner join table2 on table2.table1_id = table1.Id for xml auto
Check this link.
I dont know if this is better but I would probably make some variables instead of calling the linq in the xml. Then you can just call the variable in the xml document.
Something like this maybe:
private static void CreateDatabaseXml(string path)
{
tbchrDataContext db = new tbchrDataContext();
var rARefNum = db.T_ORDER_DETAILs.Select(i => i.RARefNum).Single();
var customer = db.T_ORDER_HEADERs.Select(i => i.Customer).Single();
var supplierName = db.T_ORDER_DETAILs.Select(i => i.SupplierName).Single();
XDocument doc = new XDocument(
// XML Declaration
new XDeclaration("1.0", "utf-8", "yes"),
// XML Root element to 3rd in nest
new XElement(ns + "WMS",
new XElement(ns + "Order",
new XElement(ns + "Header", new XElement(ns + "RARefNum", rARefNum),
new XElement (ns + "WMSCategory", customer),
new XElement (ns + "CustomerID", supplierName)))) );
#endregion
doc.Save(path);
}
I am trying to generate an XML using XDocument by pulling data from a DataTable at runtime. I want to have the output in this format:
<Document>
<Alphabets>
<Data>
<Capital>AAA</Capital>
<Small>aaa</Small>
</Data>
</Alphabets>
<Language>
<Name>English</Name>
</Language>
<Alphabets>
<Data>
<Capital>BBB</Capital>
<Small>bbb</Small>
</Data>
</Alphabets>
<Language>
<Name>English</Name>
</Language>
</Document>
The Language element has to be present after every Alphabets element. I have tried very hard to achieve this but I am unable to put this Alphabets tag after every Language element. What I have achieved is this, where the Language element is falling inside Alphabets element:
<Document>
<Alphabets>
<Data>
<Capital>AAA</Capital>
<Small>aaa</Small>
</Data>
<Language>
<Name>English</Name>
</Language>
</Alphabets>
<Alphabets>
<Data>
<Capital>BBB</Capital>
<Small>bbb</Small>
</Data>
<Language>
<Name>English</Name>
</Language>
</Alphabets>
</Document>
Here is my code :
static void Main(string[] args)
{
DataTable dtAlpha = new DataTable("Alphabetss");
dtAlpha.Columns.Add("Capital", typeof(string));
dtAlpha.Columns.Add("Small", typeof(string));
dtAlpha.Rows.Add("AAA", "aaa");
dtAlpha.Rows.Add("BBB", "bbb");
XDocument doc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("Document",
from row in dtAlpha.AsEnumerable()
select new XElement("Alphabets",
new XElement("Data",
new XElement("Capital", row.Field<string>("Capital")),
new XElement("Small", row.Field<string>("Small"))
),
new XElement("Language",
new XElement("Name", "English")
)))
);
Console.WriteLine(doc.ToString());
Console.ReadKey();
}
Please help me on this.
You can try this way :
XDocument doc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("Document")
);
foreach(var row in dtAlpha.AsEnumerable())
{
var alphabets = new XElement("Alphabets",
new XElement("Data",
new XElement("Capital", row.Field<string>("Capital")),
new XElement("Small", row.Field<string>("Small"))
)
);
var language = new XElement("Language",
new XElement("Name", "English")
);
doc.Root.Add(alphabets);
doc.Root.Add(language);
}
I have an XML Document that looks like
<Settings>
<Config>
<type>stuff</type>
<length>stuff</length>
<ID>1</ID>
</Config>
</Settings>
I am attempting to append to it using
Document loadedDoc = XDocument.Load (file);
loadedDoc.Element("Settings").Add(new XElement("Config"),
new XElement("type", "stuff"),
new XElement("length", "stuff"),
new XElement("ID", "2"));
loadedDoc.Save(file);
What I am getting when I do this is
<Settings>
//the other configs hidden for readability
<Config /> <--- WHAT DO?! 0_o
<type>stuff</type>
<length>stuff</length>
<ID>2</ID>
</Settings>
Could someone better versed in linq tell me what it is I'm doing wrong to make it create an empty config element like that? Thanks!
You need to add the sub elements through the "Config" element's constructor, rather than through the "Settings" constructor:
loadedDoc.Element("Settings").Add(new XElement("Config",
new XElement("type", "stuff"),
new XElement("length", "stuff"),
new XElement("ID", "2")));
That's because you are appending to Settings, and not to Config
Try:
loadedDoc.Element("Settings").Add(
new XElement("Config",
new XElement("type", "stuff"),
new XElement("length", "stuff"),
new XElement("ID", "2")));
try this
XDocument loadedDoc = XDocument.Load(file);
loadedDoc.Element("Settings").Add(new XElement("Config"));
loadedDoc.Element("Settings").Element("Config").Add(
new XElement("type", "stuff"),
new XElement("length", "stuff"),
new XElement("ID", "2"));
loadedDoc.Save(file);
Say I have an XML Document that looks like this:
<BOOK>
<NAME>Home</NAME>
<ISBN>0-943396-04-2</ISBN>
<PRICE>$0.82</PRICE>
</BOOK>
And I have a program that has the purpose of allowing the user to add another book to the list... what code would I put on the Button I have titled "Add Book" to make it add another set of information?
So the end product would look like this:
<BOOK>
<NAME>Home</NAME>
<ISBN>0-943396-04-2</ISBN>
<PRICE>$0.82</PRICE>
</BOOK>
<BOOK>
<NAME>Work</NAME>
<ISBN>0-85131-041-9</ISBN>
<PRICE>$0.99</PRICE>
</BOOK>
I am using Microsoft Visual C# 2010 Express, if that helps.
firstly youll need to put a container around your books list so
<BOOKS>
<BOOK>
<NAME>Home</NAME>
<ISBN>0-943396-04-2</ISBN>
<PRICE>$0.82</PRICE>
</BOOK>
</BOOKS>
Then you'll need to parse it with something are you storing it in a file? if so then
XElement.Load(filename);
otherwise you can parse a string
XElement el = XElement.Parse(#"<BOOKS><BOOK>
<NAME>Home</NAME>
<ISBN>0-943396-04-2</ISBN>
<PRICE>$0.82</PRICE>
</BOOK></BOOKS>");
then create a and add a new book
var newBook = new XElement("BOOK", new[]
{
new XElement("NAME", "thename"),
new XElement("ISBN", "isbn"),
new XElement("PRICE", ".71")
});
el.Add(newBook);
and save it if you need to
el.Save(filename)
Reformatted comment below;
var el = XElement.Load("Ops.xml");
var newOp = new XElement("Operation", new[] {
new XElement("Operation Name", textBox2.Text),
new XElement("Operation Date", dateTimePicker1.Value)
});
el.Add(newOp);
I'm trying to create a RSS 2.0 feed in ASP.NET C# with products to provide to Froogle.
The RSS feed should look like:
http://www.google.com/support/merchants/bin/answer.py?answer=160589&hl=en
I'm using the SyndicationFeed and SyndicationsItems to create the feed. But I'm having trouble adding the extra elements like g:image_link.
I try the extra elements like;
syndicationItem.ElementExtensions.Add(new XElement("image_link", product.ImageLink).CreateReader());
This works, but how can I add the namespace
xmlns:g="http://base.google.com/ns/1.0"
to the first RSS tag and use this for the extension elements?
Thank you
I just wrote something like this last week, as a matter of fact. I didn't have much time, so it's not optimized or pretty.
I used an XDocument, though.
static XDocument GetXDocument(List<GoogleProduct> googleProducts)
{
XNamespace gns = "http://base.google.com/ns/1.0";
XDocument document = new XDocument(
new XElement("rss",
new XAttribute("version", "2.0"),
new XAttribute(XNamespace.Xmlns + "g", gns),
new XElement("channel",
new XElement("title", "X Company Feed"),
new XElement("description", "X Description"),
new XElement("link", "http://www.somecompany.com/"),
from googleProduct in googleProducts
select new XElement("item",
new XElement("title", googleProduct.Title),
new XElement(gns + "brand", googleProduct.ProductRecommendedAttributes.Brand),
new XElement(gns + "manufacturer", googleProduct.ProductRecommendedAttributes.Manufacturer),
new XElement(gns + "condition", googleProduct.Condition),
new XElement("description", googleProduct.Description),
new XElement(gns + "id", googleProduct.ID),
from img in googleProduct.ProductRecommendedAttributes.ImageLinks
select new XElement(gns + "image_link", img),
new XElement("link", googleProduct.Link),
new XElement(gns + "price", googleProduct.Price.ToString("0.00")),
new XElement(gns + "product_type", googleProduct.ProductRecommendedAttributes.ProductType),
from pmt in googleProduct.ProductOptionalAttributes.PaymentAccepteds
select new XElement(gns + "payment_accepted", pmt)))));
//
return document;
}
(FYI: GoogleProduct is just a temporary mapper class I used)
It will generate a document along these lines
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title>Blah Data Feed</title>
<description>Stuff from Blah</description>
<link>http://www.blah.com/shopping</link>
<item>
<title>Blah</title>
<g:brand>Blah</g:brand>
<g:manufacturer>Blah</g:manufacturer>
<g:condition>New</g:condition>
<description>blah blah</description>
<g:id>268</g:id>
<g:image_link>http://www.blah.com/shopping/images/PRODUCT/medium/268.jpg</g:image_link>
<link>http://www.blah.com/</link>
<g:price>1747.00</g:price>
<g:product_type>Blah Blah</g:product_type>
<g:payment_accepted>Cash</g:payment_accepted>
<g:payment_accepted>Check</g:payment_accepted>
<g:payment_accepted>Visa</g:payment_accepted>
<g:payment_accepted>Mastercard</g:payment_accepted>
</item>
<item>
<title>Blah</title>
<g:brand>Blah</g:brand>
<g:manufacturer>Blah</g:manufacturer>
<g:condition>New</g:condition>
<description>blah blah</description>
<g:id>269</g:id>
<g:image_link>http://www.blah.com/shopping/images/PRODUCT/medium/269.jpg</g:image_link>
<link>http://www.blah.com/</link>
<g:price>1103.00</g:price>
<g:product_type>blah blah</g:product_type>
<g:payment_accepted>Cash</g:payment_accepted>
<g:payment_accepted>Check</g:payment_accepted>
<g:payment_accepted>Visa</g:payment_accepted>
<g:payment_accepted>Mastercard</g:payment_accepted>
</item>
</channel>
</rss>
XElements have great namespace support. Create your first element like this:
XNamespace aw = "http://base.google.com/ns/1.0";
XElement root = new XElement(aw + "image_link", product.ImageLink);
This will give you XML like this:
<image_link xmlns="http://base.google.com/ns/1.0">
</image_link>
Each subsequent element should also use the same namespace. If you want to use namespace prefixes for your elements, it's a similar approach. You can check out some full examples on MSDN here:
How to: Create a Document with Namespaces